3 个解决方案
#1
不太明白,顶下!
#2
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.*;
public class LayoutTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
private JPanel p = null; // 放按钮的JPanel
private CardLayout card = null; // CardLayout布局管理器
private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮
private MyPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel
private HashMap<String,MyPanel> map = new HashMap<String,MyPanel>();
public LayoutTest() {
super("CardLayout Test");
try {
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
/**创建一个具有指定的水平和垂直间隙的新卡片布局*/
card = new CardLayout(5, 5);
pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout
p = new JPanel(); // 构造放按钮的JPanel
b_1 = new JButton("1");
b_2 = new JButton("2");
b_3 = new JButton("3");
b_1.setMargin(new Insets(2,2,2,2));
b_2.setMargin(new Insets(2,2,2,2));
b_3.setMargin(new Insets(2,2,2,2));
p.add(b_1);
p.add(b_2);
p.add(b_3);
p_1 = new MyPanel(Color.RED,"JPanel_1");
p_2 = new MyPanel(Color.BLUE,"JPanel_2");
p_3 = new MyPanel(Color.GREEN,"JPanel_3");
pane.add(p_1, "p1");
pane.add(p_2, "p2");
pane.add(p_3, "p3");
map.put("p1", p_1);
map.put("p2", p_2);
map.put("p3", p_3);
b_1.addActionListener(new ActionListener() { // 直接翻转到p_1
public void actionPerformed(ActionEvent e) {
card.show(pane, "p1");
map.get("p1").field1.requestFocus();
}
});
b_2.addActionListener(new ActionListener() { // 直接翻转到p_2
public void actionPerformed(ActionEvent e) {
card.show(pane, "p2");
map.get("p2").field1.requestFocus();
}
});
b_3.addActionListener(new ActionListener() { // 直接翻转到p_3
public void actionPerformed(ActionEvent e) {
card.show(pane, "p3");
map.get("p3").field1.requestFocus();
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
p_1.field1.requestFocus();
}
public static void main(String[] args) {
new LayoutTest();
}
class MyPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField field1;
public JTextField field2;
public MyPanel(Color color,String Label){
super();
this.setBackground(color);
field1 = new JTextField("field1",10);
field2 = new JTextField("field2",10);
this.add(field1);
this.add(field2);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.*;
public class LayoutTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
private JPanel p = null; // 放按钮的JPanel
private CardLayout card = null; // CardLayout布局管理器
private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮
private MyPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel
private HashMap<String,MyPanel> map = new HashMap<String,MyPanel>();
public LayoutTest() {
super("CardLayout Test");
try {
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
/**创建一个具有指定的水平和垂直间隙的新卡片布局*/
card = new CardLayout(5, 5);
pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout
p = new JPanel(); // 构造放按钮的JPanel
b_1 = new JButton("1");
b_2 = new JButton("2");
b_3 = new JButton("3");
b_1.setMargin(new Insets(2,2,2,2));
b_2.setMargin(new Insets(2,2,2,2));
b_3.setMargin(new Insets(2,2,2,2));
p.add(b_1);
p.add(b_2);
p.add(b_3);
p_1 = new MyPanel(Color.RED,"JPanel_1");
p_2 = new MyPanel(Color.BLUE,"JPanel_2");
p_3 = new MyPanel(Color.GREEN,"JPanel_3");
pane.add(p_1, "p1");
pane.add(p_2, "p2");
pane.add(p_3, "p3");
map.put("p1", p_1);
map.put("p2", p_2);
map.put("p3", p_3);
b_1.addActionListener(new ActionListener() { // 直接翻转到p_1
public void actionPerformed(ActionEvent e) {
card.show(pane, "p1");
map.get("p1").field1.requestFocus();
}
});
b_2.addActionListener(new ActionListener() { // 直接翻转到p_2
public void actionPerformed(ActionEvent e) {
card.show(pane, "p2");
map.get("p2").field1.requestFocus();
}
});
b_3.addActionListener(new ActionListener() { // 直接翻转到p_3
public void actionPerformed(ActionEvent e) {
card.show(pane, "p3");
map.get("p3").field1.requestFocus();
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
p_1.field1.requestFocus();
}
public static void main(String[] args) {
new LayoutTest();
}
class MyPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField field1;
public JTextField field2;
public MyPanel(Color color,String Label){
super();
this.setBackground(color);
field1 = new JTextField("field1",10);
field2 = new JTextField("field2",10);
this.add(field1);
this.add(field2);
}
}
}
#3
似乎requestFocus()方法要求他的上级容器要先获得焦点,所以最好写在后面,写在页面不再变化的地方
#1
不太明白,顶下!
#2
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.*;
public class LayoutTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
private JPanel p = null; // 放按钮的JPanel
private CardLayout card = null; // CardLayout布局管理器
private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮
private MyPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel
private HashMap<String,MyPanel> map = new HashMap<String,MyPanel>();
public LayoutTest() {
super("CardLayout Test");
try {
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
/**创建一个具有指定的水平和垂直间隙的新卡片布局*/
card = new CardLayout(5, 5);
pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout
p = new JPanel(); // 构造放按钮的JPanel
b_1 = new JButton("1");
b_2 = new JButton("2");
b_3 = new JButton("3");
b_1.setMargin(new Insets(2,2,2,2));
b_2.setMargin(new Insets(2,2,2,2));
b_3.setMargin(new Insets(2,2,2,2));
p.add(b_1);
p.add(b_2);
p.add(b_3);
p_1 = new MyPanel(Color.RED,"JPanel_1");
p_2 = new MyPanel(Color.BLUE,"JPanel_2");
p_3 = new MyPanel(Color.GREEN,"JPanel_3");
pane.add(p_1, "p1");
pane.add(p_2, "p2");
pane.add(p_3, "p3");
map.put("p1", p_1);
map.put("p2", p_2);
map.put("p3", p_3);
b_1.addActionListener(new ActionListener() { // 直接翻转到p_1
public void actionPerformed(ActionEvent e) {
card.show(pane, "p1");
map.get("p1").field1.requestFocus();
}
});
b_2.addActionListener(new ActionListener() { // 直接翻转到p_2
public void actionPerformed(ActionEvent e) {
card.show(pane, "p2");
map.get("p2").field1.requestFocus();
}
});
b_3.addActionListener(new ActionListener() { // 直接翻转到p_3
public void actionPerformed(ActionEvent e) {
card.show(pane, "p3");
map.get("p3").field1.requestFocus();
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
p_1.field1.requestFocus();
}
public static void main(String[] args) {
new LayoutTest();
}
class MyPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField field1;
public JTextField field2;
public MyPanel(Color color,String Label){
super();
this.setBackground(color);
field1 = new JTextField("field1",10);
field2 = new JTextField("field2",10);
this.add(field1);
this.add(field2);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.*;
public class LayoutTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
private JPanel p = null; // 放按钮的JPanel
private CardLayout card = null; // CardLayout布局管理器
private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮
private MyPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel
private HashMap<String,MyPanel> map = new HashMap<String,MyPanel>();
public LayoutTest() {
super("CardLayout Test");
try {
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
/**创建一个具有指定的水平和垂直间隙的新卡片布局*/
card = new CardLayout(5, 5);
pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout
p = new JPanel(); // 构造放按钮的JPanel
b_1 = new JButton("1");
b_2 = new JButton("2");
b_3 = new JButton("3");
b_1.setMargin(new Insets(2,2,2,2));
b_2.setMargin(new Insets(2,2,2,2));
b_3.setMargin(new Insets(2,2,2,2));
p.add(b_1);
p.add(b_2);
p.add(b_3);
p_1 = new MyPanel(Color.RED,"JPanel_1");
p_2 = new MyPanel(Color.BLUE,"JPanel_2");
p_3 = new MyPanel(Color.GREEN,"JPanel_3");
pane.add(p_1, "p1");
pane.add(p_2, "p2");
pane.add(p_3, "p3");
map.put("p1", p_1);
map.put("p2", p_2);
map.put("p3", p_3);
b_1.addActionListener(new ActionListener() { // 直接翻转到p_1
public void actionPerformed(ActionEvent e) {
card.show(pane, "p1");
map.get("p1").field1.requestFocus();
}
});
b_2.addActionListener(new ActionListener() { // 直接翻转到p_2
public void actionPerformed(ActionEvent e) {
card.show(pane, "p2");
map.get("p2").field1.requestFocus();
}
});
b_3.addActionListener(new ActionListener() { // 直接翻转到p_3
public void actionPerformed(ActionEvent e) {
card.show(pane, "p3");
map.get("p3").field1.requestFocus();
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
p_1.field1.requestFocus();
}
public static void main(String[] args) {
new LayoutTest();
}
class MyPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField field1;
public JTextField field2;
public MyPanel(Color color,String Label){
super();
this.setBackground(color);
field1 = new JTextField("field1",10);
field2 = new JTextField("field2",10);
this.add(field1);
this.add(field2);
}
}
}
#3
似乎requestFocus()方法要求他的上级容器要先获得焦点,所以最好写在后面,写在页面不再变化的地方