从其他类获取Jcomponent更改帧大小 [英] getting Jcomponent from other class changes frame size
问题描述
您好我尝试从其他类获取jcomponent。组件正在显示,但我的帧大小发生了变化。
Hello I am try to get jcomponent from other class.the components are displaying but my frame size changes.
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 ,帧大小减少任何建议,以便屏幕大小保持不变。
when I run frame open as defined size but when I select a from combo box the frame size decreases any suggestion so as screen size remain same.
推荐答案
解决这个问题的快速而肮脏的方法是摆脱 pack()
和替代 revalidate()
和重绘
:
The quick and dirty way to solve this is to get rid of pack()
and subsitute revalidate()
and repaint
:
add(panel);
revalidate();
repaint();
// pack();
-
revalidate
告诉布局管理员重新布局他们的组件。 -
重绘
请求重新绘制组件,尤其是脏区域 -
pack
告诉窗口重新布局所有组件并调整大小到最佳尺寸。 revalidate
tells the layout managers to re-lay out their components.repaint
requests that the component be repainted, especially "dirty" regionspack
tells the window to re-lay out all components and resize to the optimal size.
使用CardLayout要好得多,窗口和组件大小将保持不变,大到足以容纳最大的组件。 ...并避免使用空布局。
Much better would be to use a CardLayout, and the window and component size will remain constant, big enough to fit the largest component. ... and avoid all use of null layouts.
例如:
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();
}
});
}
}
这样可以简化事情并使自己更难拍摄脚。所以不是这样:
This simplifies things and makes it harder to shoot yourself in the foot. So rather than this:
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方法就是这样:
Your ActionListener's actionPerformed method is just this:
public void actionPerformed(ActionEvent e) {
JComboBox<String> combo = (JComboBox<String>)e.getSource();
String item = combo.getSelectedItem().toString();
cardLayout.show(Example2.this, item);
}
有关CardLayout的更多信息,其用途和功能,请查看 CardLayout教程。但简而言之,它可以自动化在GUI中交换视图的过程,从而使自己更难以在脚下拍摄。
For more on the CardLayout, its uses and functionality, please check out the CardLayout Tutorial. But in a nutshell, it automates the process of swapping "views" in a GUI, making it much harder to shoot yourself in the foot.
这篇关于从其他类获取Jcomponent更改帧大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!