我试图在另一个类中设置String的值,然后从第三个类中获取它。现在我正在尝试使用set和get方法,但我无法让它工作!当用户启动程序时,会出现一个按钮。按下按钮时,测试字符串得到值" Hello"。然后我调用set类并在参数中输入test。现在在set方法中它是一个System.out.println并且它打印Hello,但是当我试图从我的第二个类中获取该方法时,它打印出null。我不知道为什么!这是我的课程:
public class Class1 {
public static void main(String[] args) {
Class3 c3 = new Class3();
c3.setFilename("Hello");
JFrame frame = new JFrame("Action Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JButton button = new JButton("Click me!");
frame.add(button);
Class2 c2 = new Class2();
button.addActionListener(c2);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
这是我的第二个类,它是我想要的下一个类的文件名:
public class Class2 implements ActionListener {
String filename;
public void actionPerformed(ActionEvent e) {
Class3 c3 = new Class3();
filename = c3.getFilename();
System.out.println(filename); // Prints null
}
}
以下是第三类:
public class Class3 {
String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
System.out.println(filename); // Prints Hello
}
}
答案 0 :(得分:2)
您的问题包括:
fileName = something
?)例如,您可以将JButton字符串名称和文件名封装在类或枚举中。使用类的优点是,您可以从文件中读取数据并从文件数据创建类的对象,从而提供额外的灵活性,但是出于我的示例的目的,并且为了避免添加额外的文件,I&#39 ; ll使用枚举,类似
public enum ButtonFileName {
MONDAY("Monday", KeyEvent.VK_M, "monday_blues.jpg"),
TUESDAY("Tuesday", KeyEvent.VK_T, "tuesday_fun.jpg"),
WEDNESDAY("Wednesday", KeyEvent.VK_W, "humpday.jpg"),
THURSDAY("Thursday", KeyEvent.VK_H, "almost_friday.jpg");
private String buttonText;
private String fileName;
private int mnemonic;
private ButtonFileName(String buttonText, int mnemonic, String fileName) {
this.buttonText = buttonText;
this.mnemonic = mnemonic;
this.fileName = fileName;
}
public String getButtonText() {
return buttonText;
}
public int getMnemonic() {
return mnemonic;
}
public String getFileName() {
return fileName;
}
}
然后我可以给我的主类一个ButtonFileName变量,selectedButtonFileName,然后在按下按钮时填充该变量。我也可以创建自己的Actions,每个ButtonFileName枚举一个,并使用这些Actions来设置我的按钮。例如:
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class Home2 extends JPanel {
private static final String TRAIN_PANEL = "train panel";
private static final String BTN_ROW_PANEL = "btn row panel";
private CardLayout cardLayout = new CardLayout();
private ButtonFileName selectedButtonFileName = null;
private JPanel buttonRowPanel = new JPanel();
private JPanel trainPanel = new JPanel();
public Home2() {
buttonRowPanel = createButtonRowPanel();
trainPanel = createTrainPanel();
setLayout(cardLayout);
add(buttonRowPanel, buttonRowPanel.getName());
add(trainPanel, trainPanel.getName());
}
private JPanel createButtonRowPanel() {
JPanel btnRowPanel = new JPanel(new GridLayout(1, 0, 5, 5));
btnRowPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
btnRowPanel.setName(BTN_ROW_PANEL);
for (ButtonFileName btnFileName : ButtonFileName.values()) {
JButton btn = new JButton(new RowButtonAction(btnFileName, this));
btnRowPanel.add(btn);
}
return btnRowPanel;
}
private JPanel createTrainPanel() {
JPanel trainPanel = new JPanel();
trainPanel.setName(TRAIN_PANEL);
JButton trainBtn = new JButton(new TrainAction("Train", KeyEvent.VK_T, this));
trainPanel.add(trainBtn);
return trainPanel;
}
public ButtonFileName getSelectedButtonFileName() {
return selectedButtonFileName;
}
public void setSelectedButtonFileName(ButtonFileName selectedButtonFileName) {
this.selectedButtonFileName = selectedButtonFileName;
}
public void nextCardLayoutView() {
cardLayout.next(this);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Home2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Home2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
再次枚举
enum ButtonFileName {
MONDAY("Monday", KeyEvent.VK_M, "monday_blues.jpg"),
TUESDAY("Tuesday", KeyEvent.VK_T, "tuesday_fun.jpg"),
WEDNESDAY("Wednesday", KeyEvent.VK_W, "humpday.jpg"),
THURSDAY("Thursday", KeyEvent.VK_H, "almost_friday.jpg");
private String buttonText;
private String fileName;
private int mnemonic;
private ButtonFileName(String buttonText, int mnemonic, String fileName) {
this.buttonText = buttonText;
this.mnemonic = mnemonic;
this.fileName = fileName;
}
public String getButtonText() {
return buttonText;
}
public int getMnemonic() {
return mnemonic;
}
public String getFileName() {
return fileName;
}
}
我对火车JButton的行动
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
@SuppressWarnings("serial")
public class TrainAction extends AbstractAction {
private Home2 home;
public TrainAction(String name, int mnemonic, Home2 home) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.home = home;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected File Name: "
+ home.getSelectedButtonFileName().getFileName());
home.nextCardLayoutView();
}
}
我对行按钮的操作
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
@SuppressWarnings("serial")
public class RowButtonAction extends AbstractAction {
private ButtonFileName btnFileName;
private Home2 home;
public RowButtonAction(ButtonFileName btnFileName, Home2 home) {
super(btnFileName.getButtonText());
putValue(MNEMONIC_KEY, btnFileName.getMnemonic());
this.btnFileName = btnFileName;
this.home = home;
}
@Override
public void actionPerformed(ActionEvent e) {
home.setSelectedButtonFileName(btnFileName);
home.nextCardLayoutView();
}
}
至于灵活性,说我想添加另一个JButton和文件名。将星期五元素添加到枚举中并完成。