格式化 GUI/添加动作侦听器时遇到问题
Having trouble formatting GUI/ adding an action listener
我通过这个网站和 Java 网站进行了广泛的搜索,试图找出这个问题的答案,但找不到针对我的问题的答案。
我不确定将 b1
和 b2
的动作侦听器放在哪里,我还认为我使用的方法可能有问题(尽管代码仍然可以编译并运行并且在没有动作侦听器的情况下一切正常。)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CubeCalc {
public static void MakeTitlePage()
{
JButton b1 = new JButton("Start");
b1.setBackground(Color.decode("#5A20DF"));
b1.setForeground(Color.WHITE);
b1.setLayout(new GridBagLayout());
b1.setPreferredSize(new Dimension(150,50));
JButton b2 = new JButton("Information about the Developer");
b2.setBackground(Color.decode("#23D123"));
b2.setForeground(Color.BLACK);
b2.setLayout(new GridBagLayout());
b2.setPreferredSize(new Dimension(275,50));
GridBagConstraints blo = new GridBagConstraints();
blo.fill = GridBagConstraints.HORIZONTAL;
blo.gridx = 0;
blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JPanel start = new JPanel(new GridBagLayout());
start.setBackground(Color.BLACK);
start.setPreferredSize(new Dimension(300,100));
start.add(b1, blo);
JPanel info = new JPanel(new GridBagLayout());
info.setBackground(Color.BLACK);
info.setPreferredSize(new Dimension(300,100));
info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JFrame window = new JFrame("Cubic Feet Calculator"); //Creates Frame
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
window.add(start, BorderLayout.NORTH);
window.add(info, BorderLayout.SOUTH);
window.pack(); //resizes to minimum possible frame size
//window.setSize(500,500); //Sets size of frame
window.setLocationRelativeTo(null);
window.setVisible(true); //Sets the frame to be visible
window.setResizable(true);
window.setBackground(Color.BLACK);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
}
public static void main(String[] args) {
MakeTitlePage();
}
}
我应该在哪里添加我的 ActionListener
以及我应该用我的方法做什么?
您可以尝试类似的方法:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// what do you want to execute...
}
});
对该片段的评论:
- 不需要设置按钮的布局
- 使用
addActionListener
向按钮添加 ActionListener
- 使用事件调度线程,使用
SwingUtilities.invokeLater
从 main
启动框架
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CubeCalc {
public static void MakeTitlePage()
{
final JFrame window = new JFrame("Cubic Feet Calculator"); // Creates Frame (created first so the button can reference it for JOptionPane
JButton b1 = new JButton("Start");
b1.setBackground(Color.decode("#5A20DF"));
b1.setForeground(Color.WHITE);
//b1.setLayout(new GridBagLayout()); << unnecessary
b1.setPreferredSize(new Dimension(150,50));
b1.addActionListener(new ActionListener() { // action when button is pressed
int pressCount=0;
@Override
public void actionPerformed(ActionEvent e) {
switch(++pressCount) {
case 1: JOptionPane.showMessageDialog(window, "Hey, stop pressing me!"); break;
case 2: JOptionPane.showMessageDialog(window, "I said, stop pressing me!!!!"); break;
default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
}
}
});
JButton b2 = new JButton("Information about the Developer");
b2.setBackground(Color.decode("#23D123"));
b2.setForeground(Color.BLACK);
//b2.setLayout(new GridBagLayout()); << unnecessary
b2.setPreferredSize(new Dimension(275,50));
b2.addActionListener(new ActionListener() { // action when button is pressed
int pressCount=0;
@Override
public void actionPerformed(ActionEvent e) {
switch(++pressCount) {
case 1: JOptionPane.showMessageDialog(window, "There is no information here!"); break;
case 2: JOptionPane.showMessageDialog(window, "Stop asking me for information!!!!"); break;
default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
}
}
});
GridBagConstraints blo = new GridBagConstraints();
blo.fill = GridBagConstraints.HORIZONTAL;
blo.gridx = 0;
blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JPanel start = new JPanel(new GridBagLayout());
start.setBackground(Color.BLACK);
start.setPreferredSize(new Dimension(300,100));
start.add(b1, blo);
JPanel info = new JPanel(new GridBagLayout());
info.setBackground(Color.BLACK);
info.setPreferredSize(new Dimension(300,100));
info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
window.add(start, BorderLayout.NORTH);
window.add(info, BorderLayout.SOUTH);
window.setLocationRelativeTo(null);
window.setResizable(true);
window.setBackground(Color.BLACK);
window.pack(); //resizes to minimum possible frame size
window.setVisible(true); //Sets the frame to be visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { // launch frame on the Event Dispatch Thread
@Override
public void run() {
MakeTitlePage();
}
});
}
}
效果:
我通过这个网站和 Java 网站进行了广泛的搜索,试图找出这个问题的答案,但找不到针对我的问题的答案。
我不确定将 b1
和 b2
的动作侦听器放在哪里,我还认为我使用的方法可能有问题(尽管代码仍然可以编译并运行并且在没有动作侦听器的情况下一切正常。)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CubeCalc {
public static void MakeTitlePage()
{
JButton b1 = new JButton("Start");
b1.setBackground(Color.decode("#5A20DF"));
b1.setForeground(Color.WHITE);
b1.setLayout(new GridBagLayout());
b1.setPreferredSize(new Dimension(150,50));
JButton b2 = new JButton("Information about the Developer");
b2.setBackground(Color.decode("#23D123"));
b2.setForeground(Color.BLACK);
b2.setLayout(new GridBagLayout());
b2.setPreferredSize(new Dimension(275,50));
GridBagConstraints blo = new GridBagConstraints();
blo.fill = GridBagConstraints.HORIZONTAL;
blo.gridx = 0;
blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JPanel start = new JPanel(new GridBagLayout());
start.setBackground(Color.BLACK);
start.setPreferredSize(new Dimension(300,100));
start.add(b1, blo);
JPanel info = new JPanel(new GridBagLayout());
info.setBackground(Color.BLACK);
info.setPreferredSize(new Dimension(300,100));
info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JFrame window = new JFrame("Cubic Feet Calculator"); //Creates Frame
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
window.add(start, BorderLayout.NORTH);
window.add(info, BorderLayout.SOUTH);
window.pack(); //resizes to minimum possible frame size
//window.setSize(500,500); //Sets size of frame
window.setLocationRelativeTo(null);
window.setVisible(true); //Sets the frame to be visible
window.setResizable(true);
window.setBackground(Color.BLACK);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
}
public static void main(String[] args) {
MakeTitlePage();
}
}
我应该在哪里添加我的 ActionListener
以及我应该用我的方法做什么?
您可以尝试类似的方法:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// what do you want to execute...
}
});
对该片段的评论:
- 不需要设置按钮的布局
- 使用
addActionListener
向按钮添加 - 使用事件调度线程,使用
SwingUtilities.invokeLater
从
ActionListener
main
启动框架
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CubeCalc {
public static void MakeTitlePage()
{
final JFrame window = new JFrame("Cubic Feet Calculator"); // Creates Frame (created first so the button can reference it for JOptionPane
JButton b1 = new JButton("Start");
b1.setBackground(Color.decode("#5A20DF"));
b1.setForeground(Color.WHITE);
//b1.setLayout(new GridBagLayout()); << unnecessary
b1.setPreferredSize(new Dimension(150,50));
b1.addActionListener(new ActionListener() { // action when button is pressed
int pressCount=0;
@Override
public void actionPerformed(ActionEvent e) {
switch(++pressCount) {
case 1: JOptionPane.showMessageDialog(window, "Hey, stop pressing me!"); break;
case 2: JOptionPane.showMessageDialog(window, "I said, stop pressing me!!!!"); break;
default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
}
}
});
JButton b2 = new JButton("Information about the Developer");
b2.setBackground(Color.decode("#23D123"));
b2.setForeground(Color.BLACK);
//b2.setLayout(new GridBagLayout()); << unnecessary
b2.setPreferredSize(new Dimension(275,50));
b2.addActionListener(new ActionListener() { // action when button is pressed
int pressCount=0;
@Override
public void actionPerformed(ActionEvent e) {
switch(++pressCount) {
case 1: JOptionPane.showMessageDialog(window, "There is no information here!"); break;
case 2: JOptionPane.showMessageDialog(window, "Stop asking me for information!!!!"); break;
default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
}
}
});
GridBagConstraints blo = new GridBagConstraints();
blo.fill = GridBagConstraints.HORIZONTAL;
blo.gridx = 0;
blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
JPanel start = new JPanel(new GridBagLayout());
start.setBackground(Color.BLACK);
start.setPreferredSize(new Dimension(300,100));
start.add(b1, blo);
JPanel info = new JPanel(new GridBagLayout());
info.setBackground(Color.BLACK);
info.setPreferredSize(new Dimension(300,100));
info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
window.add(start, BorderLayout.NORTH);
window.add(info, BorderLayout.SOUTH);
window.setLocationRelativeTo(null);
window.setResizable(true);
window.setBackground(Color.BLACK);
window.pack(); //resizes to minimum possible frame size
window.setVisible(true); //Sets the frame to be visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { // launch frame on the Event Dispatch Thread
@Override
public void run() {
MakeTitlePage();
}
});
}
}
效果: