Card Layout本来是会切换面板的,为什么程序启动后就不会切换了呢?
Card Layout will originally switch panels, by why won't it switch after the start of the program?
所以我遇到了一种奇怪的错误。我正在使用卡片布局在 GUI 中的帮助面板和用户面板之间切换。最初启动程序时,卡片布局按预期工作,只要用户按下 "H",就会切换,但如果您单击任何按钮或在文本字段中键入,您将无法再切换卡片面板。我查看了 oracle 文档,但一无所获。有谁知道是什么导致了这个问题?
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainPanel extends JPanel implements KeyListener{
CardLayout cl = new CardLayout();
userPanel up = new userPanel();
HelpPanel hp = new HelpPanel();
private boolean showUserPanel = true;
private Timer mainTimer = new Timer(500, new ActionListener(){
public void actionPerformed(ActionEvent event){
up.setTipPercent();
}
});
//---------------------------------------------------------------------
//Constructor
public MainPanel(){
setLayout(cl);
add(up, "userPanel");
add(hp, "HelpPanel");
cl.show(this, "userPanel");
addKeyListener(this);
setFocusable(true);
mainTimer.start();
}
//---------------------------------------------------------------------
//Key Listener Methods
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == e.VK_H && showUserPanel == true){
cl.show(this, "HelpPanel");
mainTimer.stop();
showUserPanel = false;
}else if(keyCode == e.VK_H && showUserPanel == false){
cl.show(this, "userPanel");
showUserPanel = true;
mainTimer.start();
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
这是userPanel
Class
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//---------------------------------------------------------------------
public class userPanel extends JPanel{
//Fields
private JButton calc;
private JRadioButton fiveP, tenP, fiftP, twenP;
private ButtonGroup buttonGroup;
private double tipAmount = 0.0;
private JTextField tipArea;
private JTextArea totalArea;
//---------------------------------------------------------------------
//Constructor
public userPanel(){
setBackground(Color.BLACK);
tipArea = new JTextField("Enter bill amount");
totalArea = new JTextArea("");
setupButtons();
tipArea.setPreferredSize(new Dimension(300,40));
totalArea.setPreferredSize(new Dimension(300,40));
}
//---------------------------------------------------------------------
//Setup JComponents
public void setupButtons(){
calc = new JButton("Calculate the Tip");
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
calculateTheTip(tipAmount);
}
});
buttonGroup = new ButtonGroup();
fiveP = new JRadioButton("Five Percent");
tenP = new JRadioButton("Ten Percent");
fiftP = new JRadioButton("Fifteen Percent");
twenP = new JRadioButton("Twenty Percent");
buttonGroup.add(fiveP);
buttonGroup.add(tenP);
buttonGroup.add(fiftP);
buttonGroup.add(twenP);
add(fiveP);
add(tenP);
add(fiftP);
add(twenP);
add(tipArea);
add(totalArea);
add(calc);
}
//---------------------------------------------------------------------
//Calculate the total and display it to the user
public void calculateTheTip(double total){
String theTotal = tipArea.getText();
//Take user input and make sure it is a number
try{
double billAmount = Double.parseDouble(theTotal);
total = billAmount*total;
double totalAmt = billAmount + billAmount*tipAmount;
totalArea.setText("Tip: "+total+ " \nTotal: " + totalAmt);
}catch(NumberFormatException E){
totalArea.setText("Please Enter only the amount without a $ or text!");
}
return;
}
//---------------------------------------------------------------------
//Chose which tipPercent to use
public void setTipPercent(){
if(fiveP.isSelected()){
tipAmount = .05;
}else if(tenP.isSelected()){
tipAmount = .1;
}else if(fiftP.isSelected()){
tipAmount = .15;
}else{
tipAmount = .2;
}
}
}
这是Help Panel
class
import java.awt.*;
import javax.swing.JPanel;
public class HelpPanel extends JPanel{
public HelpPanel(){
setBackground(Color.BLACK);
}
}
按按钮等操作会将焦点从 MainPanel
上移开。 KeyEvent
被发送到具有焦点的对象而不是 MainPanel
。
我建议将 KeyListener
创建为一个单独的对象并将其添加到其他组件中。
您的程序无法知道单击 H
是否意味着按下卡片更换键,或者您是否正在写入 JTextField
。
对于您的问题,请使用键绑定,这是最好的解决方案,因为它将焦点更改为您想要关注的组件。阅读关于此的 docs。
所以我遇到了一种奇怪的错误。我正在使用卡片布局在 GUI 中的帮助面板和用户面板之间切换。最初启动程序时,卡片布局按预期工作,只要用户按下 "H",就会切换,但如果您单击任何按钮或在文本字段中键入,您将无法再切换卡片面板。我查看了 oracle 文档,但一无所获。有谁知道是什么导致了这个问题?
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainPanel extends JPanel implements KeyListener{
CardLayout cl = new CardLayout();
userPanel up = new userPanel();
HelpPanel hp = new HelpPanel();
private boolean showUserPanel = true;
private Timer mainTimer = new Timer(500, new ActionListener(){
public void actionPerformed(ActionEvent event){
up.setTipPercent();
}
});
//---------------------------------------------------------------------
//Constructor
public MainPanel(){
setLayout(cl);
add(up, "userPanel");
add(hp, "HelpPanel");
cl.show(this, "userPanel");
addKeyListener(this);
setFocusable(true);
mainTimer.start();
}
//---------------------------------------------------------------------
//Key Listener Methods
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == e.VK_H && showUserPanel == true){
cl.show(this, "HelpPanel");
mainTimer.stop();
showUserPanel = false;
}else if(keyCode == e.VK_H && showUserPanel == false){
cl.show(this, "userPanel");
showUserPanel = true;
mainTimer.start();
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
这是userPanel
Class
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//---------------------------------------------------------------------
public class userPanel extends JPanel{
//Fields
private JButton calc;
private JRadioButton fiveP, tenP, fiftP, twenP;
private ButtonGroup buttonGroup;
private double tipAmount = 0.0;
private JTextField tipArea;
private JTextArea totalArea;
//---------------------------------------------------------------------
//Constructor
public userPanel(){
setBackground(Color.BLACK);
tipArea = new JTextField("Enter bill amount");
totalArea = new JTextArea("");
setupButtons();
tipArea.setPreferredSize(new Dimension(300,40));
totalArea.setPreferredSize(new Dimension(300,40));
}
//---------------------------------------------------------------------
//Setup JComponents
public void setupButtons(){
calc = new JButton("Calculate the Tip");
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
calculateTheTip(tipAmount);
}
});
buttonGroup = new ButtonGroup();
fiveP = new JRadioButton("Five Percent");
tenP = new JRadioButton("Ten Percent");
fiftP = new JRadioButton("Fifteen Percent");
twenP = new JRadioButton("Twenty Percent");
buttonGroup.add(fiveP);
buttonGroup.add(tenP);
buttonGroup.add(fiftP);
buttonGroup.add(twenP);
add(fiveP);
add(tenP);
add(fiftP);
add(twenP);
add(tipArea);
add(totalArea);
add(calc);
}
//---------------------------------------------------------------------
//Calculate the total and display it to the user
public void calculateTheTip(double total){
String theTotal = tipArea.getText();
//Take user input and make sure it is a number
try{
double billAmount = Double.parseDouble(theTotal);
total = billAmount*total;
double totalAmt = billAmount + billAmount*tipAmount;
totalArea.setText("Tip: "+total+ " \nTotal: " + totalAmt);
}catch(NumberFormatException E){
totalArea.setText("Please Enter only the amount without a $ or text!");
}
return;
}
//---------------------------------------------------------------------
//Chose which tipPercent to use
public void setTipPercent(){
if(fiveP.isSelected()){
tipAmount = .05;
}else if(tenP.isSelected()){
tipAmount = .1;
}else if(fiftP.isSelected()){
tipAmount = .15;
}else{
tipAmount = .2;
}
}
}
这是Help Panel
class
import java.awt.*;
import javax.swing.JPanel;
public class HelpPanel extends JPanel{
public HelpPanel(){
setBackground(Color.BLACK);
}
}
按按钮等操作会将焦点从 MainPanel
上移开。 KeyEvent
被发送到具有焦点的对象而不是 MainPanel
。
我建议将 KeyListener
创建为一个单独的对象并将其添加到其他组件中。
您的程序无法知道单击 H
是否意味着按下卡片更换键,或者您是否正在写入 JTextField
。
对于您的问题,请使用键绑定,这是最好的解决方案,因为它将焦点更改为您想要关注的组件。阅读关于此的 docs。