您好我尝试从其他类获取jcomponent。组件正在显示,但我的帧大小发生了变化。
Example.java
public class Example extends JFrame{
static JPanel panel = new JPanel();
static A a = new A();
static B b = new B();
static JComboBox<String> combo = new JComboBox<>();
static String value;
Example(){
setSize(400, 400);
combo.setBounds(450, 140, 50, 20);
combo.addItem("");
combo.addItem("a");
combo.addItem("b");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
value = (String) combo.getSelectedItem().toString();
if(value.equals("a")){
panel.add(a.getLabel());
panel.remove(b.getLabel());
add(panel);
pack();
}else if(value.equals("b")){
panel.add(b.getLabel());
panel.remove(a.getLabel());
add(panel);
pack();
}
}
});
panel.add(combo);
this.add(panel);
setVisible(true);
}
}
A.java
public class A extends JFrame{
JPanel panel = new JPanel();
JLabel lab = new JLabel("Text");
A(){
lab.setBounds(280, 25, 150, 50);
lab.setVisible(true);
panel.add(lab);
add(panel);
}
public JLabel getLabel(){
return lab;
}
}
B.java
public class B extends JFrame{
JPanel panel = new JPanel();
JButton lab = new JButton("Hello");
B(){
lab.setBounds(380, 25, 250, 50);
lab.setVisible(true);
panel.add(lab);
add(panel);
}
public JButton getLabel(){
return lab;
}
}
Main.java
public class Main {
public static void main(String [] agrs) {
Example ex = new Example();
ex.setVisible(true);
}
}
当我按照定义的尺寸打开框架但是当我从组合框中选择 a 时,框架尺寸会减少任何建议,因此屏幕尺寸保持不变。
答案 0 :(得分:2)
解决这个问题的快捷方法是摆脱pack()
并替换revalidate()
和repaint
:
add(panel);
revalidate();
repaint();
// pack();
revalidate
告诉布局管理员重新布局他们的组件。repaint
要求重新绘制组件,特别是&#34;脏&#34;区域pack
告诉窗口重新布局所有组件并调整大小到最佳尺寸。更好的方法是使用CardLayout,窗口和组件大小将保持不变,大到足以容纳最大的组件。 ...并避免使用空布局。
例如:
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Example2 extends JPanel {
public static final String[] COMBO_TEXTS = {"", "a", "b"};
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private DefaultComboBoxModel<String > comboModel = new DefaultComboBoxModel<>(COMBO_TEXTS);
private CardLayout cardLayout = new CardLayout();
public Example2() {
setLayout(cardLayout);
ComboListener comboListener = new ComboListener();
JComboBox<String> combo = new JComboBox<>(comboModel);
combo.addActionListener(comboListener);;
JPanel panelBlank = new JPanel();
panelBlank.add(combo);
JPanel panelWithText = new JPanel();
combo = new JComboBox<>(comboModel);
combo.addActionListener(comboListener);;
panelWithText = new JPanel();
panelWithText.add(combo);
panelWithText.add(new JLabel("Text"));
JPanel panelWithButton = new JPanel();
combo = new JComboBox<>(comboModel);
combo.addActionListener(comboListener);;
panelWithButton = new JPanel();
panelWithButton.add(combo);
panelWithButton.add(new JButton("Hello"));
add(panelBlank, COMBO_TEXTS[0]);
add(panelWithText, COMBO_TEXTS[1]);
add(panelWithButton, COMBO_TEXTS[2]);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class ComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox<String> combo = (JComboBox<String>)e.getSource();
String item = combo.getSelectedItem().toString();
cardLayout.show(Example2.this, item);
}
}
private static void createAndShowGui() {
Example2 mainPanel = new Example2();
JFrame frame = new JFrame("Example2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这简化了事情,并且更难以自己射击。所以不是这样:
public void actionPerformed(ActionEvent e) {
value = (String) combo.getSelectedItem().toString();
if(value.equals("a")){
panel.add(a.getLabel());
panel.remove(b.getLabel());
// add(panel);
// pack();
}else if(value.equals("b")){
panel.add(b.getLabel());
panel.remove(a.getLabel());
// add(panel);
// pack();
}
add(panel);
revalidate();
repaint();
}
你的ActionListener的actionPerformed方法就是这样:
public void actionPerformed(ActionEvent e) {
JComboBox<String> combo = (JComboBox<String>)e.getSource();
String item = combo.getSelectedItem().toString();
cardLayout.show(Example2.this, item);
}
有关CardLayout及其用途和功能的更多信息,请查看CardLayout Tutorial。但简而言之,它可以自动化交换过程和视图。在图形用户界面中,让自己更难用脚射击。
答案 1 :(得分:1)
尝试使用revalidate()
代替pack()
; pack()
会自动调整框架大小以适应子组件:
包装()的javadoc:
使此窗口的大小适合首选大小和布局 其子组件。窗口的最终宽度和高度是 如果任何一个尺寸小于,则自动放大 上一次调用setMinimumSize指定的最小大小 方法