所以我创建了一个包含三个按钮的菜单,每个按钮都会打开另一个窗口。我将按钮添加到 actionListnener,我检查了 Source,我做了所有事情。但它仍然是一个虚拟按钮。
说够了,我认为如果你们看到代码会更好。先感谢您!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;
public class MainMenu extends JFrame
{
private JPanel contentPane;
private JButton decB;
private JButton hexB;
private JButton binB;
private JLabel label1;
public MainMenu()
{
setTitle("Hello Dear Friend");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 323, 303);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setLocationRelativeTo( null );
JButton decB = new JButton("Decimal");
decB.setFont(new Font("Tahoma", Font.BOLD, 12));
decB.setBounds(79, 85, 146, 42);
decB.setBackground( new Color( 212, 208, 199 ) );
decB.setFocusPainted( false );
JButton hexB = new JButton("Hexadecimal");
hexB.setFont(new Font("Tahoma", Font.BOLD, 12));
hexB.setBounds(79, 138, 146, 42);
hexB.setBackground( new Color( 212, 208, 199 ) );
hexB.setFocusPainted( false );
JButton binB = new JButton("Binary");
binB.setFont(new Font("Tahoma", Font.BOLD, 12));
binB.setBounds(79, 191, 146, 42);
binB.setBackground( new Color( 212, 208, 199 ) );
binB.setFocusPainted( false );
JLabel label1 = new JLabel("Select the base you wish to convert: ");
label1.setFont(new Font("Courier New", Font.BOLD, 12));
label1.setBounds(20, 11, 277, 63);
contentPane.add(decB);
contentPane.add(binB);
contentPane.add(hexB);
contentPane.add(label1);
ButtonHandler bh = new ButtonHandler();
decB.addActionListener( bh );
hexB.addActionListener( bh );
binB.addActionListener( bh );
}
private class ButtonHandler implements ActionListener
{
DecMenu dm = new DecMenu();
BinaryMenu bm = new BinaryMenu();
HexMenu hm = new HexMenu();
public void actionPerformed( ActionEvent event)
{
setVisible( false );
if( event.getSource() == decB )
dm.setVisible(true);
else if( event.getSource() == hexB)
hm.setVisible( true );
else if( event.getSource() == binB )
bm.setVisible( true );
}
}
}