请帮助我了解如何让我的JButton多次工作?
截至目前我只能在每次点击按钮时替换面板一次,但是需要这样做:当用户按btnNext我要更改面板,然后在另一个btnNext按下更改面板后,同样适用于btnBack
谢谢。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
public class beta1 extends JFrame implements ItemListener, ActionListener {
private final JButton btnNext, btnBack;
private JButton btnNext2;
private JButton btnAbout;
private final JRadioButton rb1, rb2, rb3, rb4, rb5, rb6;
private final JCheckBox chk1, chk2, chk3, chk4, chk5, chk6, chk7, chk8;
private final JComboBox<String> cboAColor, cboBColor, cboCColor, cboProvinces;
private final JPanel mainpanel = new JPanel(), southpanel = new JPanel(),
eastpanel = new JPanel(), westpanel = new JPanel(), centerpanel = new JPanel(),
northpanel = new JPanel(), centerpanelsouth = new JPanel(), centerpanelnorth = new JPanel(),
centerpanelcenter = new JPanel();
private final JLabel label = new JLabel(), label1 = new JLabel(), label2 = new JLabel(),
label3 = new JLabel(), label4 = new JLabel(), label5 = new JLabel();
// private JLabel button = new JLabel(), button1 = new JLabel();
private final ImageIcon imgIcon1 = new ImageIcon("S80.png"), imgIcon2 = new ImageIcon("S60.png"),
imgIcon3 = new ImageIcon("V60.png"), imgIcon4 = new ImageIcon("XC90.png"),
imgIcon5 = new ImageIcon("XC70.png"), imgIcon6 = new ImageIcon("XC60.png");
private final ImageIcon img1 = new ImageIcon("Black Sapphire.png"), img2 = new ImageIcon(
"Electric Silver.png"), img3 = new ImageIcon("Passion Red.png"), img4 = new ImageIcon(
"Crystal White Pearl.png");
private final ImageIcon im1 = new ImageIcon("Black ARTICO Leather.png"), im2 = new ImageIcon(
"Alpaca Grey ARTICO Leather.png"), im3 = new ImageIcon("Beige ARTICO Leather.png");
private final ImageIcon i1 = new ImageIcon("Burl Walnut Wood.png"), i2 = new ImageIcon(
"Dark Ash Wood.png"), i3 = new ImageIcon("Aluminium.png");
private final JLabel txt1 = new JLabel(), txt2 = new JLabel(), txt3 = new JLabel(),
txt4 = new JLabel(), txt5 = new JLabel(), txt6 = new JLabel(), txt7 = new JLabel(),
txt8 = new JLabel(), txt9 = new JLabel(), txt10 = new JLabel(), txt11 = new JLabel();
private double m, total, total1, ftotal, extra, colors, n1, n2, n3, extra1;
private final JTabbedPane tab = new JTabbedPane();
int i = 1;
double O = 0;
double Q = 0;
double N = 0;
double NB = 0;
double M = 0;
double A = 0;
double BC = 0;
double PEI = 0;
double S = 0;
double NL = 0;
double SEL = 0;
private final Color bcolor = new Color(159, 182, 205);
public static void main(String[] args)
{
new beta1();
}
public beta1()
{
// NEXT
btnNext = new JButton(">>");
btnNext.addActionListener(new ButtonListener());
// BACK
btnBack = new JButton("<<");
btnBack.addActionListener(new ButtonListener());
// RADIOBUTTON//
rb1 = new JRadioButton("Vovlo S80");
rb2 = new JRadioButton("Vovlo S60");
rb3 = new JRadioButton("Vovlo V60");
rb4 = new JRadioButton("Volvo XC90");
rb5 = new JRadioButton("Volvo XC70");
rb6 = new JRadioButton("Volvo XC60");
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
group.add(rb4);
group.add(rb5);
group.add(rb6);
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
rb4.addItemListener(this);
rb5.addItemListener(this);
rb6.addItemListener(this);
// END OF RADIOBUTTON//
// JCOMBOBOXBUTTON//
String[] aa = { "Black Sapphire", "Electric Silver", "Passion Red [add $200.00]",
"Crystal White Pearl [add $200.00]" };
cboAColor = new JComboBox<String>(aa);
cboAColor.setPreferredSize(new Dimension(300, 20));
cboAColor.addActionListener(this);
String[] bb = { "Black/Black ARTICO Leather, without additional charge",
"Alpaca Grey/Alpaca Grey ARTICO Leather, without additional charge",
"Almond Beige/Cashmere Beige ARTICO Leather [add $350.00]" };
cboBColor = new JComboBox<String>(bb);
cboBColor.setPreferredSize(new Dimension(300, 20));
cboBColor.addActionListener(this);
String[] cc = { "Burl Walnut Wood, without additional charge",
"Dark Ash Wood, without additional charge", "Aluminum[add $200]" };
cboCColor = new JComboBox<String>(cc);
cboCColor.setPreferredSize(new Dimension(300, 20));
cboCColor.addActionListener(this);
String[] dd = { "Select Province", "Ontario", "Quebeck", "Nova Scotia", "New Brunswick",
"Manitoba", "British Colombia", "Prince Edward Island", "Saskatchewan", "Alberta",
"Newfoundland and Labrador" };
cboProvinces = new JComboBox<String>(dd);
cboProvinces.addActionListener(this);
// END OF JCOMBOBOX//
// JCHECKBOXBUTTON //
chk1 = new JCheckBox("Climate Package [Add $1,350.00]");
chk2 = new JCheckBox("Technology Package [Add $1,500.00]");
chk3 = new JCheckBox("Inscription Package [Add $2,600.00]");
chk4 = new JCheckBox("19\" BOR Diamond Cut Alloy Wheels [Add $1,250.00]");
chk5 = new JCheckBox("Blind Spot Information System [Add $800.00]");
chk6 = new JCheckBox("Active Dual Xenon Headlights with Washers [Add $1,000.00]");
chk7 = new JCheckBox("Front and Rear Park Assist [Add $600.00]");
chk8 = new JCheckBox("Apple CarPlay System + Bose Sound System [Add $2400.00]");
chk1.addItemListener(this);
chk2.addItemListener(this);
chk3.addItemListener(this);
chk4.addItemListener(this);
chk5.addItemListener(this);
chk6.addItemListener(this);
chk7.addItemListener(this);
chk8.addItemListener(this);
// END OF JCHECKBOXBUTTON//
// LABEL IMAGE/TEXT/BORDER
// EMPTY
ImageIcon imgThisImg = new ImageIcon("volvo.png");
label.setIcon(imgThisImg);
label.setOpaque(false);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
// /END
// JRADIOBUTTON NORTH
northpanel.setLayout(new FlowLayout());
northpanel.add(rb1);
northpanel.add(rb4);
northpanel.add(rb5);
northpanel.add(rb6);
northpanel.setBackground(new Color(255, 255, 255));
// END
// JBUTTON SOUTH
southpanel.setLayout(new FlowLayout());
southpanel.add(btnBack);
southpanel.add(btnNext);
southpanel.setBackground(new Color(255, 255, 255));
// END
// JCOMBOBOXBUTTON WEAST
westpanel.setLayout(new GridLayout(3, 1, 0, 0));
westpanel.add(cboAColor);
westpanel.add(cboBColor);
westpanel.add(cboCColor);
westpanel.setBackground(new Color(255, 255, 255));
// END
// JCHECKBOXBUTTON EAST
eastpanel.setLayout(new GridLayout(9, 1, 0, 0));
eastpanel.add(chk1);
eastpanel.add(chk2);
eastpanel.add(chk3);
eastpanel.add(chk4);
eastpanel.add(chk5);
eastpanel.add(chk6);
eastpanel.add(chk7);
eastpanel.add(chk8);
eastpanel.add(txt6);
eastpanel.setBackground(new Color(255, 255, 255));
// END
// LABEL CENTER
centerpanel.setLayout(new BorderLayout());
centerpanel.add(label, BorderLayout.CENTER);
centerpanel.setBackground(new Color(255, 255, 255));
// END
// MAIN PANEL
mainpanel.setLayout(new BorderLayout());
mainpanel.add(southpanel, BorderLayout.SOUTH);
mainpanel.add(northpanel, BorderLayout.NORTH);
mainpanel.add(centerpanel, BorderLayout.CENTER);
// END
// frame
setContentPane(mainpanel);
setSize(480, 960);
setTitle("Volvo Store");
getContentPane().setBackground(new Color(255, 255, 255));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
validate();
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnNext) {
remove(centerpanel);
remove(northpanel);
add(westpanel);
validate();
} else if (e.getSource() == btnBack) {
remove(westpanel);
add(centerpanel);
add(northpanel);
validate();
}
}
}
@Override
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == rb1) {
label.setIcon(imgIcon1);
centerpanel.add(label);
} else if (rb4.isSelected()) {
label.setIcon(imgIcon4);
centerpanel.add(label);
} else if (rb5.isSelected()) {
label.setIcon(imgIcon5);
centerpanel.add(label);
} else if (rb6.isSelected()) {
label.setIcon(imgIcon6);
centerpanel.add(label);
}
}
}
答案 0 :(得分:6)
您可以通过以下方式更改按钮单击操作:
nextButton.setAction(myAction)
[doc]
或者通过实施自定义ActionListener
。
对于您的MainPanel,您可以使用CardLayout。
使用CardLayout,您可以执行以下操作:
nextButton.addActionListener(new ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout)(MainPanel.getLayout());
cl.next();
}
}
答案 1 :(得分:2)
每次按下两个导航按钮时,repaint()
后validate()
拨打{{1}}。另外,请参阅this question以获得有关这些方法的详细说明。