这就是我正在工作的看看StudentIDPrint的JLabel打印出来的那个,还有StudentIDSS用于带有textfield的字符串是StudentIDSS应该从中得到它的价值我真的很想得到任何提示和帮助谢谢你很多我认为这可能是代码的位置,但我真的迷失在这里,任何帮助都会很可爱,非常感谢你们
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Allows it too use JFrame
public class LiftingApp extends JFrame {
//Calls them from import (like calling a Scanner)
private JLabel EnterSID;
private JButton button;
private JTextField textfield;
private ImageIcon image;
private JLabel WaukeeAPEX;
private ImageIcon ThankIMG;
private JLabel Thank;
private JButton AdminButton;
private JTextField AdminText;
private JButton AdminButton2;
private JLabel AdminThank;
private JButton goback;
private ImageIcon Login;
private ImageIcon AdminLogin;
private ImageIcon RTMS;
private JLabel StudentIDPrint;
public String StudentIDSS;
public LiftingApp()
{
getContentPane().setBackground(Color.GRAY);
setIconImage(Toolkit.getDefaultToolkit().getImage(LiftingApp.class.getResource("ICON.jpg")));
getContentPane().setLayout(new FlowLayout());
goback = new JButton("Return To Main Screen");
getContentPane().add(goback);
goback.setVisible(false);
event4 rb = new event4();
goback.addActionListener(rb);
AdminThank = new JLabel("Thank You For Loging in");
AdminThank.setForeground(Color.WHITE);
getContentPane().add(AdminThank);
AdminThank.setVisible(false);
AdminButton2 = new JButton("Admin Login");
getContentPane().add(AdminButton2);
AdminButton2.setVisible(false);
event3 al2 = new event3();
AdminButton2.addActionListener(al2);
AdminButton = new JButton("Admin Login");
getContentPane().add(AdminButton);
AdminText = new JTextField(10);
getContentPane().add(AdminText);
AdminText.setVisible(false);
event2 al = new event2();
AdminButton.addActionListener(al);
//Adds a Button
button = new JButton("Login");
getContentPane().add(button);
//Adds Text
EnterSID = new JLabel("Enter StudentID");
EnterSID.setForeground(Color.WHITE);
getContentPane().add(EnterSID);
event e = new event();
button.addActionListener(e);
//Adds Field (The box you type stuff in)
textfield = new JTextField(10);
getContentPane().add(textfield);
textfield.getText();
//Adds Image
image = new ImageIcon(getClass() .getResource("testt.png"));
//Sets Image to the WaukeeAPEX JLabel
WaukeeAPEX = new JLabel(image);
getContentPane().add(WaukeeAPEX);
ThankIMG = new ImageIcon(getClass() .getResource("testt2.png"));
Thank = new JLabel(ThankIMG);
getContentPane().add(Thank);
Thank.setVisible(false);
StudentIDPrint = new JLabel();
getContentPane().add(StudentIDPrint);
StudentIDPrint.setVisible(false);
StudentIDPrint.setText("Student IDS Are:" + StudentIDSS);
}
public static void main(String[] args)
{
//This is just talking about the window
LiftingApp gui = new LiftingApp();
//When you click the X it closes...
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Size of intial box (pack puts every thing you have to the smallest size to keep everything)
gui.setSize(1000, 1000);
//This makes it so you can see the Window
gui.setVisible(true);
//This Sets The Title
gui.setTitle("Elmin Strength");
//Dont Worry About any of this just learning Arrays
//Array Initializer
}
//ActionListener Just waits for the action to happen then dose what is told
public class event implements ActionListener {
//Telling what the Action is
public void actionPerformed(ActionEvent e) {
StudentIDSS = textfield.getText();
//This will show after the button is pressed
EnterSID.setVisible(false);
textfield.setVisible(true);
Thank.setVisible(true);
AdminText.setVisible(false);
AdminButton.setVisible(false);
button.setVisible(false);
WaukeeAPEX.setVisible(false);
textfield.setVisible(false);
goback.setVisible(true);
}
}
public class event2 implements ActionListener{
public void actionPerformed(ActionEvent al){
textfield.setVisible(false);
EnterSID.setVisible(false);
button.setVisible(false);
AdminText.setVisible(true);
AdminButton.setVisible(false);
AdminButton2.setVisible(true);
goback.setVisible(true);
}
}
public class event3 implements ActionListener{
public void actionPerformed(ActionEvent al2){
String p = AdminText.getText();
AdminButton2.setVisible(false);
textfield.setVisible(false);
AdminText.setVisible(true);
AdminText.getSelectedText();
WaukeeAPEX.setVisible(false);
if(p.compareTo("Password") == 0) {
Thank.setVisible(true);
goback.setVisible(true);
AdminThank.setVisible(true);
StudentIDPrint.setVisible(true);
}else{
goback.setVisible(true);
AdminText.setVisible(false);
}
}
}
public class event4 implements ActionListener{
public void actionPerformed(ActionEvent rb) {
goback.setVisible(false);
button.setVisible(true);
EnterSID.setVisible(true);
textfield.setVisible(true);
AdminButton.setVisible(true);
WaukeeAPEX.setVisible(true);
Thank.setVisible(false);
AdminButton2.setVisible(false);
AdminThank.setVisible(false);
AdminText.setVisible(false);
}
}
}
答案 0 :(得分:0)
我不知道你在玩LiftingApp的是什么 为了方便,我重新排列了一些代码。
reference
现在,您有一个FlowLayout作为您的类的布局,包含4个按钮和4个动作侦听器。
只有一个侦听器就足够了。
我只是将每个侦听器的代码作为MainEvent类放到一个侦听器中。如您所知,有几种方法可以使用动作侦听器。这可能是一种方法。
public LiftingApp() {
// skip...
getContentPane().setLayout(new FlowLayout());
getContentPane().add(goback);
getContentPane().add(AdminThank);
getContentPane().add(AdminButton2);
getContentPane().add(AdminButton);
getContentPane().add(AdminText);
getContentPane().add(button);
getContentPane().add(EnterSID);
getContentPane().add(textfield);
getContentPane().add(WaukeeAPEX);
getContentPane().add(Thank);
getContentPane().add(StudentIDPrint);
event4 rb = new event4();
event3 al2 = new event3();
event2 al = new event2();
event e = new event();
goback.addActionListener(rb);
AdminButton2.addActionListener(al2);
AdminButton.addActionListener(al);
button.addActionListener(e);
goback.setVisible(false);
AdminThank.setVisible(false);
AdminButton2.setVisible(false);
AdminText.setVisible(false);
Thank.setVisible(false);
StudentIDPrint.setVisible(false);
}
然后,您可以按如下方式调用主事件类;
class MainButtonEvt implements ActionListener
{
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("====> ");
if(evt.getSource() == (JButton) button)
{
System.out.println("====> login");
StudentIDSS = textfield.getText();
StudentIDPrint.setText("Student IDS Are:" + StudentIDSS);
if(!StudentIDPrint.isVisible()) StudentIDPrint.setVisible(true);
// This will show after the button is pressed
if(EnterSID.isVisible()) EnterSID.setVisible(false);
if(!textfield.isVisible()) textfield.setVisible(true);
if(!Thank.isVisible()) Thank.setVisible(true);
if(AdminText.isVisible()) AdminText.setVisible(false);
if(AdminButton.isVisible()) AdminButton.setVisible(false);
if(button.isVisible()) button.setVisible(false);
if(WaukeeAPEX.isVisible()) WaukeeAPEX.setVisible(false);
if(AdminThank.isVisible()) AdminThank.setVisible(false);
if(!goback.isVisible()) goback.setVisible(true);
}
if(evt.getSource() == (JButton) goback)
{
System.out.println("====> goback");
String p = textfield.getText();
if(p != null && !p.equals("")) textfield.setText("");
if(goback.isVisible()) goback.setVisible(false);
if(!button.isVisible()) button.setVisible(true);
if(!EnterSID.isVisible()) EnterSID.setVisible(true);
if(textfield.isVisible()) textfield.setVisible(false);
if(!AdminButton.isVisible()) AdminButton.setVisible(true);
if(!WaukeeAPEX.isVisible()) WaukeeAPEX.setVisible(true);
if(Thank.isVisible()) Thank.setVisible(false);
if(AdminButton2.isVisible()) AdminButton2.setVisible(false);
if(AdminThank.isVisible()) AdminThank.setVisible(false);
if(!AdminText.isVisible()) AdminText.setVisible(true);
}
if(evt.getSource() == (JButton) AdminButton)
{
System.out.println("====> AdminButton");
if(AdminButton2.isVisible()) AdminButton2.setVisible(false);
if(textfield.isVisible()) textfield.setVisible(false);
if(!AdminText.isVisible()) AdminText.setVisible(true);
String p = AdminText.getText();
//p = AdminText.getSelectedText();
if(WaukeeAPEX.isVisible()) WaukeeAPEX.setVisible(false);
if (p.compareTo("Password") == 0) {
if(!Thank.isVisible()) Thank.setVisible(true);
if(!goback.isVisible()) goback.setVisible(true);
if(!AdminThank.isVisible()) AdminThank.setVisible(true);
if(!StudentIDPrint.isVisible())
StudentIDPrint.setVisible(true);
} else {
if(!goback.isVisible()) goback.setVisible(true);
if(AdminText.isVisible()) AdminText.setVisible(false);
}
}
}
}
我希望你能找到如何处理它。 完整源代码:
MainButtonEvt mainEvt = new MainButtonEvt();
goback.addActionListener(mainEvt);
AdminButton2.addActionListener(mainEvt);
AdminButton.addActionListener(mainEvt);
button.addActionListener(mainEvt);
答案 1 :(得分:0)
首先很抱歉发布我的整个代码,其中我做的是制作一个从JTextField获取的JLabel然后我创建了一个从JLabel获取的字符串
StudentIDSSS = new JLabel(": " + textfield.getText() + ":");
getContentPane().add(StudentIDSSS);
StudentIDSSS.setVisible(true);
String List = StudentIDSSS.getText();
System.out.print(List);
StudentIDList = new JLabel(List);
getContentPane().add(StudentIDList);
StudentIDList.setVisible(false);