请耐心等待我,但我对Java swing非常陌生,并且花了好几天时间试图解决这个问题。我有一个带两个面板的框架。第一个面板有两个按钮(“我正在简化”):“新建”和“打开”。当帧最初出现时,第二个面板显示空的JCombobox,JCombobox是setEnable(false)。目的是让用户选择“新建”或“打开”,如果用户按下“新建”,则第二个面板中的组件转换为JTextField,如果用户按下“打开”,则保留为JComboBox。 JComboBox的项目是从数据库填充的,填充了Combobox正在运行。我的问题是试图弄清楚如何将第二个面板中的组件转换为JTextField或JCombobox。我尝试使用removeallItems并将setPopupVisible设置为false,使组合框看起来像JTextField,但这似乎不起作用。我不断得到一个带有下拉箭头的组件,当点击它时会显示一个空行,看起来很奇怪。我想禁止下拉显示空行,或将组件转换为简单的JTextField。任何帮助都会真正得到应用。
public class newButtonlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
clearFields();
newButton.setEnabled(false);
openButton.setEnabled(false);
calcButton.setEnabled(false);
tableButton.setEnabled(false);
saveButton.setEnabled(false);
textField1.setBackground(Color.WHITE);
textField2.setBackground(Color.WHITE);
textField3.setBackground(Color.WHITE);
setDisable(textField2);
textField3.setEnabled(false);
table.setEnabled(false);
textField1.setEnabled(true);
textField1.removeAllItems();
textField1.setPopupVisible(false);
BasicComboPopup popup = new BasicComboPopup( textField1 );
popup.setPopupSize(0, 0);
textField1.setEditable(true);
textField1.requestFocus();
}
}
答案 0 :(得分:2)
使用构图。在使用CardLayout的JPanel中有一个JTextField和一个JComboBox(因此,一次只显示一个)。当需要切换时,请告诉面板布局切换显示的组件。
答案 1 :(得分:0)
使用secondPanel
// first proposition
// I suppose the secondPanel contains the combobox
secondPanel.removeAll();
secondPanel.add(new JTextField());
// and vice-versa
//second proposition
// they are both in secondPanel: toogle
combobox.setVisible(true);
textfield.setVisible(false);
//and vice-versa
答案 2 :(得分:0)
我认为这是相当接近的。我使用GroupLayout作为单独的JPanel卡。我唯一无法弄清楚的是如何在调整框架大小时阻止组件调整大小。任何建议我将不胜感激。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.LEADING;
import java.awt.Dimension;
public class dataTableSummaryCards implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String OPENPANEL = "Open";
final static String NEWPANEL = "New";
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { OPENPANEL, NEWPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards"
JComboBox openDataSummary = new JComboBox();
Dimension dim=openDataSummary.getSize();
// openDataSummary.setMaximumSize(dim);
openDataSummary.setPreferredSize(dim);
openDataSummary.setMinimumSize(dim);
JLabel label1 = new JLabel("Data Summary Name:");
JLabel label2 = new JLabel("Data Summary Name:");
JPanel openCard = new JPanel();
GroupLayout layout = new GroupLayout(openCard);
openCard.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(label1,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE ,GroupLayout.DEFAULT_SIZE))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(openDataSummary,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(label1,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE)
.addComponent(openDataSummary,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAU LT_SIZE,GroupLayout.DEFAULT_SIZE)))
;
JTextField newDataSummary = new JTextField();
newDataSummary.setSize(dim);
newDataSummary.setPreferredSize(dim);
// newDataSummary.setMaximumSize(dim);
newDataSummary.setMinimumSize(dim);
JPanel newCard = new JPanel();
GroupLayout layout2 = new GroupLayout(newCard);
newCard.setLayout(layout2);
layout2.setAutoCreateGaps(true);
layout2.setAutoCreateContainerGaps(true);
layout2.setHorizontalGroup(layout2.createSequentialGroup()
.addGroup(layout2.createParallelGroup(LEADING)
.addComponent(label2,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE))
.addGroup(layout2.createParallelGroup(LEADING)
.addComponent(newDataSummary,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE))
);
layout2.setVerticalGroup(layout2.createSequentialGroup()
.addGroup(layout2.createParallelGroup()
.addComponent(label2,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE)
.addComponent(newDataSummary,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE)))
;
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(openCard, OPENPANEL);
cards.add(newCard, NEWPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Data Summary Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
dataTableSummaryCards demo = new dataTableSummaryCards();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}