[英]How to Contract Card Layout
I've a mainPanel with VerticalLayout (org.jdesktop.swingx.VerticalLayout)
. 我有一个带有VerticalLayout
(org.jdesktop.swingx.VerticalLayout)
的mainPanel 。 main panel has several sub panels. 主面板有几个子面板。 One of them is a dynamically changing panel according to the user selection.
其中之一是根据用户选择动态更改的面板。 So, I've set it's layout as
CardLayout
which I think is the easiest (maybe best?) way to achieve that. 因此,我将其布局设置为
CardLayout
,我认为这是实现该目标的最简单(也许是最好的方法)。
I'll call that panel elasticPanel . 我称该面板为elasticPanel 。 As the name says, it should be elastic.
顾名思义,它应该具有弹性。 Which means, it should be capable of both expanding and contracting.
也就是说,它应该能够扩展和收缩。 Let's say it behave like this.
假设它的行为像这样。 If the user select
1
, the elasticPanel should display one , say, JComboBox
. 如果用户选择
1
,那么elasticPanel应该显示一个,例如JComboBox
。 If the user select 2
then, two JComboBox
s... 如果用户选择
2
则两个JComboBox
...
Ok, it works perfectly up to now. 好的,到现在为止,它都可以正常工作。 Now the user select
1
again when the elasticPanel
is showing two JComboBox
s. 现在,当
elasticPanel
显示两个JComboBox
时,用户再次选择1
。 What i need to happen now is the elasticPanel
should display one JComboBox
with it's normal size. 我现在需要做的是
elasticPanel
应该显示一个具有正常大小的JComboBox
。 But since the elasticPanel
is already expanded, what happens is it shows the JComboBox
stretched to fit it's size. 但是由于
elasticPanel
已经扩展,所以发生的事情是它显示JComboBox
拉伸以适合其大小。 So it gives a weird look. 因此它看起来很奇怪。
Following screenshots show the problem i'm having with my interface. 以下屏幕截图显示了我的界面存在的问题。
Before the selection. 在选择之前。
NONE
is selected. NONE
选择。
An element is selected 选择一个元素
NONE
is selected again 再次选择
NONE
I need the elasticPanel
( Location of Fault ) in the last screenshot to be as in the first screenshot. 我需要最后一个屏幕截图中的
elasticPanel
( 故障位置 )与第一个屏幕截图相同。 This is just a simple case. 这只是一个简单的例子。 Imagine the look when going back to
NONE
after displaying about 5, 6 sub components. 想象一下,在显示约5、6个子组件后返回
NONE
时的外观。
I've tried the setSize()
method. 我已经尝试过
setSize()
方法。 It doesn't do anything..So how to fix the issue? 它什么也没做。.那么如何解决此问题?
Any help is appreciated. 任何帮助表示赞赏。 Thanks!
谢谢!
It's hard to tell you are referring CardLayout
to what thingy. 很难告诉您将
CardLayout
指向什么。 Since CardLayout works in a different way. 由于CardLayout的工作方式不同。 What you can do is simply place one
JPanel
say basePanel having GridLayout(0, 1)
and place this JPanel
on top of another JPanel
say contentPanel , and now set this as the content pane for the JFrame
and calling pack() when you add or remove an element from the view. 你可以做的是简单地将一个
JPanel
说有basePanel GridLayout(0, 1)
并把此JPanel
另一个顶部JPanel
说的ContentPanel,现在将此作为内容窗格中JFrame
和调用pack()当您添加或从视图中删除一个元素。 Here is one example showing you what I mean. 这是一个示例,向您展示我的意思。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ElasticPanel
{
private JFrame frame;
private JPanel contentPane;
private JPanel basePanel;
/*
* Array to hold the JComboBox
* elements.
*/
private JComboBox[] prodCombo;
private JComboBox[] temp;
/*
* Counter to keep track
* of the number of JComboBox
* present.
*/
private int counter;
/*
* Data for each JComboBox
*/
private String[] data = {
"None",
"Sub Category"
};
private ActionListener comboAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JComboBox cbox = (JComboBox) ae.getSource();
String command = (String) ae.getActionCommand();
int index = Integer.parseInt(command);
String selection = (String) cbox.getSelectedItem();
if (selection.equals("None"))
{
/*
* i = index + 1, because, we want to
* remove all JComboBox after this one.
*/
for (int i = (index + 1); i < prodCombo.length; i++)
{
temp = new JComboBox[prodCombo.length];
for (int j = 0; j < prodCombo.length; j++)
temp[j] = prodCombo[j];
basePanel.remove(prodCombo[i]);
}
prodCombo = new JComboBox[index + 1];
for (int i = 0; i <= index; i++)
{
prodCombo[i] = temp[i];
}
counter = prodCombo.length;
System.out.println("Item Removed\nCounter : " + counter);
}
else if (selection.equals("Sub Category"))
{
counter++;
temp = new JComboBox[counter];
for (int i = 0; i < prodCombo.length; i++)
{
temp[i] = prodCombo[i];
}
temp[counter - 1] = new JComboBox(data);
temp[counter - 1].setActionCommand("" + (counter - 1));
temp[counter - 1].addActionListener(this);
prodCombo = new JComboBox[counter];
for (int i = 0; i < counter; i++)
prodCombo[i] = temp[i];
basePanel.add(prodCombo[counter - 1]);
System.out.println("Item Added\nCounter : " + counter);
}
//basePanel.revalidate();
//basePanel.repaint();
frame.pack();
}
};
public ElasticPanel()
{
prodCombo = new JComboBox[1];
counter = 1;
}
private void displayGUI()
{
frame = new JFrame("Elastic Panel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
basePanel = new JPanel(new GridLayout(0, 1, 5, 5));
prodCombo[counter - 1] = new JComboBox(data);
prodCombo[counter - 1].setActionCommand("" + (counter - 1));
prodCombo[counter - 1].addActionListener(comboAction);
basePanel.add(prodCombo[counter - 1]);
contentPane.add(basePanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new ElasticPanel().displayGUI();
}
});
}
}
* Latest Update : * * 最新更新:*
More insight by adding more components and placing the elastic panel at some other location, and not on top of content pane. 通过添加更多组件并将弹性面板放置在其他位置而不是内容窗格的顶部,可以获得更多的见解。
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
public class VirtualViewGUI extends JFrame
{
private JPanel rightPanel;
private ElasticPanel elasticPanel;
public VirtualViewGUI()
{
super("Virtual View");
JMenuBar jmenuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");
JMenu feel = new JMenu("Look & Feel");
JMenu layOutMenu = new JMenu("ConfigureCells");
JMenuItem add_files = new JMenuItem("Select Directory..");
JMenuItem minCellSize = new JMenuItem("height 260 X width 260");
JMenuItem moderateCellSize = new JMenuItem("height 320 X width 320");
JMenuItem maxCellSize = new JMenuItem("height 360 X width 360");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem help = new JMenuItem("Help Content");
fileMenu.add(add_files);
fileMenu.add(exit);
layOutMenu.add(minCellSize);
layOutMenu.add(moderateCellSize);
layOutMenu.add(maxCellSize);
helpMenu.add(help);
jmenuBar.add(fileMenu);
jmenuBar.add(layOutMenu);
jmenuBar.add(helpMenu);
ImageIcon myImage = null;
try
{
myImage = new ImageIcon(
new URL("http://gagandeepbali.uk.to/" +
"gaganisonline/images/swing/" +
"stackoverflow/cow-cartoon.jpg"));
}
catch(MalformedURLException mue)
{
mue.printStackTrace();
}
JLabel icon = new JLabel(myImage);
icon.setIcon(myImage);
setJMenuBar(jmenuBar);
rightPanel = new JPanel();
elasticPanel = new ElasticPanel(this);
rightPanel.add(elasticPanel);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
contentPane.add(icon, BorderLayout.CENTER);
contentPane.add(rightPanel, BorderLayout.LINE_END);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setVisible(true);
System.out.println("File Separator is : " + System.getProperty("file.separator"));
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new VirtualViewGUI();
}
});
}
}
class ElasticPanel extends JPanel
{
private JFrame frame;
private JPanel contentPane;
/*
* Array to hold the JComboBox
* elements.
*/
private JComboBox[] prodCombo;
private JComboBox[] temp;
/*
* Counter to keep track
* of the number of JComboBox
* present.
*/
private int counter;
/*
* Data for each JComboBox
*/
private String[] data = {
"None",
"Sub Category"
};
private ActionListener comboAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JComboBox cbox = (JComboBox) ae.getSource();
String command = (String) ae.getActionCommand();
int index = Integer.parseInt(command);
String selection = (String) cbox.getSelectedItem();
if (selection.equals("None"))
{
/*
* i = index + 1, because, we want to
* remove all JComboBox after this one.
*/
for (int i = (index + 1); i < prodCombo.length; i++)
{
temp = new JComboBox[prodCombo.length];
for (int j = 0; j < prodCombo.length; j++)
temp[j] = prodCombo[j];
remove(prodCombo[i]);
}
prodCombo = new JComboBox[index + 1];
for (int i = 0; i <= index; i++)
{
prodCombo[i] = temp[i];
}
counter = prodCombo.length;
System.out.println("Item Removed\nCounter : " + counter);
}
else if (selection.equals("Sub Category"))
{
counter++;
temp = new JComboBox[counter];
for (int i = 0; i < prodCombo.length; i++)
{
temp[i] = prodCombo[i];
}
temp[counter - 1] = new JComboBox(data);
temp[counter - 1].setActionCommand("" + (counter - 1));
temp[counter - 1].addActionListener(this);
prodCombo = new JComboBox[counter];
for (int i = 0; i < counter; i++)
prodCombo[i] = temp[i];
add(prodCombo[counter - 1]);
System.out.println("Item Added\nCounter : " + counter);
}
//revalidate();
//repaint();
frame.pack();
}
};
public ElasticPanel(JFrame frame)
{
this.frame = frame;
prodCombo = new JComboBox[1];
counter = 1;
setLayout(new GridLayout(0, 1, 5, 5));
prodCombo[counter - 1] = new JComboBox(data);
prodCombo[counter - 1].setActionCommand("" + (counter - 1));
prodCombo[counter - 1].addActionListener(comboAction);
add(prodCombo[counter - 1]);
}
}
If I understood you correctly, you have 2 different panels and you switch between those 2 using a CardLayout
. 如果我对您的理解正确,那么您会有2个不同的面板,并且可以使用
CardLayout
在这2个面板之间切换。 The problem with the CardLayout
is that it takes the size of the largest panel. CardLayout
的问题在于它占用了最大面板的大小。
So with the CardLayout
you will not be able to shrink the size of the container, but you can avoid that the combobox is being stretched by wrapping your panels inside another panel with a BorderLayout
, and putting the panel in the BorderLayout.NORTH
因此,使用
CardLayout
您将无法缩小容器的大小,但是可以通过将面板包装在另一个带有BorderLayout
面板内并将该面板放入BorderLayout.NORTH
来避免组合框被拉伸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.