[英]How to Contract Card Layout
我有一個帶有VerticalLayout (org.jdesktop.swingx.VerticalLayout)
的mainPanel 。 主面板有幾個子面板。 其中之一是根據用戶選擇動態更改的面板。 因此,我將其布局設置為CardLayout
,我認為這是實現該目標的最簡單(也許是最好的方法)。
我稱該面板為elasticPanel 。 顧名思義,它應該具有彈性。 也就是說,它應該能夠擴展和收縮。 假設它的行為像這樣。 如果用戶選擇1
,那么elasticPanel應該顯示一個,例如JComboBox
。 如果用戶選擇2
則兩個JComboBox
...
好的,到現在為止,它都可以正常工作。 現在,當elasticPanel
顯示兩個JComboBox
時,用戶再次選擇1
。 我現在需要做的是elasticPanel
應該顯示一個具有正常大小的JComboBox
。 但是由於elasticPanel
已經擴展,所以發生的事情是它顯示JComboBox
拉伸以適合其大小。 因此它看起來很奇怪。
以下屏幕截圖顯示了我的界面存在的問題。
在選擇之前。 NONE
選擇。
選擇一個元素
再次選擇NONE
我需要最后一個屏幕截圖中的elasticPanel
( 故障位置 )與第一個屏幕截圖相同。 這只是一個簡單的例子。 想象一下,在顯示約5、6個子組件后返回NONE
時的外觀。
我已經嘗試過setSize()
方法。 它什么也沒做。.那么如何解決此問題?
任何幫助表示贊賞。 謝謝!
很難告訴您將CardLayout
指向什么。 由於CardLayout的工作方式不同。 你可以做的是簡單地將一個JPanel
說有basePanel GridLayout(0, 1)
並把此JPanel
另一個頂部JPanel
說的ContentPanel,現在將此作為內容窗格中JFrame
和調用pack()當您添加或從視圖中刪除一個元素。 這是一個示例,向您展示我的意思。
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();
}
});
}
}
* 最新更新:*
通過添加更多組件並將彈性面板放置在其他位置而不是內容窗格的頂部,可以獲得更多的見解。
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]);
}
}
如果我對您的理解正確,那么您會有2個不同的面板,並且可以使用CardLayout
在這2個面板之間切換。 CardLayout
的問題在於它占用了最大面板的大小。
因此,使用CardLayout
您將無法縮小容器的大小,但是可以通過將面板包裝在另一個帶有BorderLayout
面板內並將該面板放入BorderLayout.NORTH
來避免組合框被拉伸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.