如何使滚动按钮默认允许按回车键?
How to make the roll button the default allowing enter press?
我编写了一个程序来随机 select 一张图像,并在每次按下按钮时将其显示在 window 中。我正在尝试弄清楚如何使 "Roll Dice" 按钮成为允许按回车键的默认按钮。
Driver:
public class MCobbM10A1 {
static int SCREEN_WIDTH = 500;
static int SCREEN_HEIGHT = 600;
static WidgetPanel widget;
static GraphicPanel myPanel;
public static void main(String[] args) {
JFrame myFrame = new JFrame("Dice Roller");
JFrame.setDefaultLookAndFeelDecorated(true);
myPanel = new GraphicPanel();
myPanel.setBounds(0, 0, 500, 200);
myFrame.add(myPanel);
JPanel lastPanel = new JPanel();
myFrame.add(lastPanel);
widget = new WidgetPanel();
widget.setBounds(0, 250, 500, 300);
myFrame.add(widget);
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed();
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
}
这是小部件面板:
public class WidgetPanel extends JPanel {
JButton rollButton;
WidgetPanel() {
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
rollButton = new JButton();
rollButton.setBounds(350, 200, 100, 25);
rollButton.setText("Roll Dice");
this.add(rollButton);
JLabel fixBug = new JLabel();
this.add(fixBug);
}
}
how to make the "Roll Dice" button the default allowing enter press.
您将默认按钮分配给框架的 JRootPane。
frame.getRootPane().setDefaultButton(…);
一种方法是:
@Override
public void addNotify()
{
super.addNotify();
JFrame frame = (JFrame)SwingUtilities.windowForComponent( rollButton );
frame.getRootPane().setDefaultButton(rollButton);
}
将面板添加到框架时调用 addNotify()
方法。
与您的问题无关,但为什么要为每个卷创建一个新对象?
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed(); // <--- new MCobbM10A1 object
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
为什么不这样做呢?
widget.rollButton.addActionListener((ae) ->
myPanel.newRoll());
我编写了一个程序来随机 select 一张图像,并在每次按下按钮时将其显示在 window 中。我正在尝试弄清楚如何使 "Roll Dice" 按钮成为允许按回车键的默认按钮。
Driver:
public class MCobbM10A1 {
static int SCREEN_WIDTH = 500;
static int SCREEN_HEIGHT = 600;
static WidgetPanel widget;
static GraphicPanel myPanel;
public static void main(String[] args) {
JFrame myFrame = new JFrame("Dice Roller");
JFrame.setDefaultLookAndFeelDecorated(true);
myPanel = new GraphicPanel();
myPanel.setBounds(0, 0, 500, 200);
myFrame.add(myPanel);
JPanel lastPanel = new JPanel();
myFrame.add(lastPanel);
widget = new WidgetPanel();
widget.setBounds(0, 250, 500, 300);
myFrame.add(widget);
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed();
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
}
这是小部件面板:
public class WidgetPanel extends JPanel {
JButton rollButton;
WidgetPanel() {
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
rollButton = new JButton();
rollButton.setBounds(350, 200, 100, 25);
rollButton.setText("Roll Dice");
this.add(rollButton);
JLabel fixBug = new JLabel();
this.add(fixBug);
}
}
how to make the "Roll Dice" button the default allowing enter press.
您将默认按钮分配给框架的 JRootPane。
frame.getRootPane().setDefaultButton(…);
一种方法是:
@Override
public void addNotify()
{
super.addNotify();
JFrame frame = (JFrame)SwingUtilities.windowForComponent( rollButton );
frame.getRootPane().setDefaultButton(rollButton);
}
将面板添加到框架时调用 addNotify()
方法。
与您的问题无关,但为什么要为每个卷创建一个新对象?
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed(); // <--- new MCobbM10A1 object
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
为什么不这样做呢?
widget.rollButton.addActionListener((ae) ->
myPanel.newRoll());