我尝试以多种方式修复此问题,并尝试在各处搜索答案,我的颜色按钮正常工作,但内置数字按钮无法正常工作。
package tralala; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; public class DugmiciKojiMenjajuBoju extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 4317497849622426733L; private JButton dugme1; private JButton dugme2; private JButton dugme3; private JPanel panel; private JOptionPane a; private JPanel panel1; private JButton dugmeBroja0; private JButton dugmeBroja1; private JButton dugmeBroja2; private JButton dugmeBroja3; private JButton dugmeBroja4; private JButton dugmeBroja5; private JButton dugmeBroja6; private JButton dugmeBroja7; private JButton dugmeBroja8; private JButton dugmeBroja9; private JButton dugmeZaPlus; private JButton dugmeZaMinus; private JButton dugmeZaPuta; private JButton dugmeZaPodeljeno; private JButton dugmeZaJednako; private JTextField polje; DugmiciKojiMenjajuBoju(){ getContentPane().setBackground(Color.white); this.setLayout(new FlowLayout()); a = new JOptionPane(""); dugme1 = new JButton("zuta"); dugme2 = new JButton("plava"); dugme3 = new JButton("crvena"); dugmeBroja0 = new JButton("0"); dugmeBroja1 = new JButton("1"); dugmeBroja2 = new JButton("2"); dugmeBroja3 = new JButton("3"); dugmeBroja4 = new JButton("4"); dugmeBroja5 = new JButton("5"); dugmeBroja6 = new JButton("6"); dugmeBroja7 = new JButton("7"); dugmeBroja8 = new JButton("8"); dugmeBroja9 = new JButton("9"); dugmeZaPlus = new JButton("+"); dugmeZaMinus = new JButton("-"); dugmeZaPuta = new JButton("*"); dugmeZaPodeljeno = new JButton("/"); dugmeZaJednako = new JButton("="); polje = new JTextField("", 20); panel = new JPanel(); panel1 = new JPanel(); dugmeBroja0.setSize(50,50); dugmeBroja1.setSize(50,50); dugmeBroja2.setSize(50,50); dugmeBroja3.setSize(50,50); dugmeBroja4.setSize(50,50); dugmeBroja5.setSize(50,50); dugmeBroja6.setSize(50,50); dugmeBroja7.setSize(50,50); dugmeBroja8.setSize(50,50); dugmeBroja9.setSize(50,50); panel1.add(dugmeBroja0); panel1.add(dugmeBroja1); panel1.add(dugmeBroja2); panel1.add(dugmeBroja3); panel1.add(dugmeBroja4); panel1.add(dugmeBroja5); panel1.add(dugmeBroja6); panel1.add(dugmeBroja7); panel1.add(dugmeBroja8); panel1.add(dugmeBroja9); panel1.add(dugmeZaPlus); panel1.add(dugmeZaMinus); panel1.add(dugmeZaPuta); panel1.add(dugmeZaPodeljeno); panel1.add(dugmeZaJednako); panel1.add(polje); panel1.setLayout(new FlowLayout(50, 0 ,0)); FlowLayout mojLayout = new FlowLayout(FlowLayout.CENTER , 20 , 20); panel.setLayout(mojLayout); panel.add(dugme1); panel.add(dugme2); panel.add(dugme3); this.setTitle("Kalkulator Koji Menja Boju Pozadine"); this.setSize(500,500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.add(panel1); this.add(panel); dugme1.addActionListener(this); dugme2.addActionListener(this); dugme3.addActionListener(this); polje.addActionListener(this); panel.setBackground(Color.white); panel1.setBackground(Color.white); } public static void main(String[] args) { DugmiciKojiMenjajuBoju a = new DugmiciKojiMenjajuBoju(); a.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { if( arg0.getSource() == dugme1){ panel.setBackground(Color.yellow); panel1.setBackground(Color.yellow); getContentPane().setBackground(Color.yellow); a.setSize(200,200); a.showMessageDialog(this, "Klikcite dalje :D" ,"Postavili ste zutu",JOptionPane.INFORMATION_MESSAGE); } else if(arg0.getSource() == dugme2){ panel.setBackground(Color.blue); panel1.setBackground(Color.blue); getContentPane().setBackground(Color.blue); a.setSize(200,200); a.showMessageDialog(this, "Klikcite dalje :D","Postavili ste plavu", JOptionPane.INFORMATION_MESSAGE); } else if(arg0.getSource() == dugme3){ panel.setBackground(Color.red); panel1.setBackground(Color.red); getContentPane().setBackground(Color.red); a.setSize(200,200); a.showMessageDialog(this, "Klikcite dalje :D","Postavili ste crvenu",JOptionPane.INFORMATION_MESSAGE); } else if(arg0.getSource() == dugmeBroja0){ polje.setText("0"); System.out.println("blabla"); } } }
我的按钮0不起作用…所以我无法继续编写代码。
您没有向按钮添加ActionListener。
但是,在解决该问题之前,您应该简化代码,并学习如何使用循环来创建具有相同功能的多个组件。这是一个创建单个ActionListener并将其添加到每个按钮的示例:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CalculatorPanel extends JPanel { private JTextField display; public CalculatorPanel() { Action numberAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // display.setCaretPosition( display.getDocument().getLength() ); display.replaceSelection(e.getActionCommand()); } }; setLayout( new BorderLayout() ); display = new JTextField(); display.setEditable( false ); display.setHorizontalAlignment(JTextField.RIGHT); add(display, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout(0, 5) ); add(buttonPanel, BorderLayout.CENTER); for (int i = 0; i < 10; i++) { String text = String.valueOf(i); JButton button = new JButton( text ); button.addActionListener( numberAction ); button.setBorder( new LineBorder(Color.BLACK) ); button.setPreferredSize( new Dimension(50, 50) ); buttonPanel.add( button ); } } private static void createAndShowUI() { JFrame frame = new JFrame("Calculator Panel"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add( new CalculatorPanel() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
另外,请勿setSize()在您的组件上使用。布局管理器负责确定组件的大小。
setSize()