Java JButton.setAction(...) null 的按钮文本
Java JButton.setAction(...) null's button text
以下代码呈现 JButton
没有文本:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = new JButton(text); // blank button rendered ???
System.out.println(button.getText()); // prints Invisible
button.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
System.out.println(button.getText()); // prints null ???
button.setFocusable(false);
button.setPreferredSize(new Dimension(100, 40));
panel.add(button);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
});
}
}
如果我删除对 button.setAction(...)
的调用,它会呈现包含文本的按钮。
或者:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = null;
button = new JButton(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
button.setText(text); // renders the button with text
System.out.println(button.getText()); // prints Invisible obviously
button.setFocusable(false);
button.setPreferredSize(new Dimension(100, 40));
panel.add(button);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
});
}
}
工作正常,但有一些令人讨厌的含义,例如 无法更改按钮操作 之后不重置其文本。
为什么?!
最好在 JButton 构造函数中输入文本,因为 JButton 构造函数的参数是字符串而不是字符串变量。
JButton button = new JButton("Invisible");
@Dima Maligin
未回复,将被删除
应声明 Swing Action(也覆盖 isEnabled,这很重要)
.
private class SwingAction extends AbstractAction {
//or public Action SwingAction() {
// return new AbstractAction("Invisible") {
// here to override AbstractAction
// }
// }
public SwingAction() {
putValue(NAME, "Invisible"); // bounds properties
putValue(SHORT_DESCRIPTION, "Invisible");
}
@Override
public void actionPerformed(ActionEvent e) {
// do nothing
}
}
以下代码呈现 JButton
没有文本:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = new JButton(text); // blank button rendered ???
System.out.println(button.getText()); // prints Invisible
button.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
System.out.println(button.getText()); // prints null ???
button.setFocusable(false);
button.setPreferredSize(new Dimension(100, 40));
panel.add(button);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
});
}
}
如果我删除对 button.setAction(...)
的调用,它会呈现包含文本的按钮。
或者:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = null;
button = new JButton(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
button.setText(text); // renders the button with text
System.out.println(button.getText()); // prints Invisible obviously
button.setFocusable(false);
button.setPreferredSize(new Dimension(100, 40));
panel.add(button);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
});
}
}
工作正常,但有一些令人讨厌的含义,例如 无法更改按钮操作 之后不重置其文本。
为什么?!
最好在 JButton 构造函数中输入文本,因为 JButton 构造函数的参数是字符串而不是字符串变量。
JButton button = new JButton("Invisible");
@Dima Maligin
未回复,将被删除
应声明 Swing Action(也覆盖 isEnabled,这很重要)
.
private class SwingAction extends AbstractAction {
//or public Action SwingAction() {
// return new AbstractAction("Invisible") {
// here to override AbstractAction
// }
// }
public SwingAction() {
putValue(NAME, "Invisible"); // bounds properties
putValue(SHORT_DESCRIPTION, "Invisible");
}
@Override
public void actionPerformed(ActionEvent e) {
// do nothing
}
}