我有一個主要的JFrame和一個額外的JFrame用於輸入許多附加參數。所以,我需要我的第二個jframe將數據返回到主jframe(使用ActionListener作爲「Save」按鈕),但我不知道我該怎麼做。請告訴我,我希望你能幫助我。謝謝。如何製作模態JFrame或其他JComponent?
2
A
回答
5
不要使第二個依賴窗口成爲JFrame。改爲使用模態JDialog或JOptionPane。許多人都沒有意識到,只需將一個擁有GUI的JPanel放入JOptionPane,JOptionPanes就可以容納複雜的GUI。
例如:
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class ComplexOptionPane extends JPanel {
private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
private JTextArea textArea = new JTextArea(12, 30);
public ComplexOptionPane() {
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton(new AbstractAction("Get Player Information") {
@Override
public void actionPerformed(ActionEvent arg0) {
int result = JOptionPane.showConfirmDialog(null, playerEditorPanel,
"Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle
.values()) {
textArea.append(String.format("%10s: %s%n",
fieldTitle.getTitle(),
playerEditorPanel.getFieldText(fieldTitle)));
}
}
}
}));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
ComplexOptionPane mainPanel = new ComplexOptionPane();
JFrame frame = new JFrame("ComplexOptionPane");
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();
}
});
}
}
@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
enum FieldTitle {
NAME("Name"), SPEED("Speed"), STRENGTH("Strength"), HEALTH("Health");
private String title;
private FieldTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
};
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
public PlayerEditorPanel() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Player Editor"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagConstraints gbc;
for (int i = 0; i < FieldTitle.values().length; i++) {
FieldTitle fieldTitle = FieldTitle.values()[i];
gbc = createGbc(0, i);
add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
gbc = createGbc(1, i);
JTextField textField = new JTextField(10);
add(textField, gbc);
fieldMap.put(fieldTitle, textField);
}
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH
: GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
public String getFieldText(FieldTitle fieldTitle) {
return fieldMap.get(fieldTitle).getText();
}
}
7
這是更好地使用JDialog比JFrame的,當你想要模式。
HERE is a great Example showing the Modality of the JDIalog.
相關問題
- 1. 如何製作模態JFrame?
- 2. 控制JFrame其他無效?
- 3. 如何製作透明的JFrame,但保持其他一切?
- 4. 如何在JComponent中繪製JComponent?
- 5. 打開JFrame時禁用其他操作
- 6. 如何在其他地方繪製隱形JFrame?
- 7. 如何從一個jframe訪問jtextarea到其他jframe
- 8. 如何讓某個JFrame與其他JFrame有不同的LAF?
- 9. 如何通過其他Jframe在一個Jframe中訪問JinternlFrme?
- 10. 如何在JOptionPane選項對話框中使用JSlider(或其他JComponent)?
- 11. 使用其他類JFrame
- 12. Java Swing - 如何從其他JPanel訪問一個JPanel的JComponent,它們都被添加到JFrame中?
- 13. 如何製作動態高度UILabel將其他元素推出?
- 14. 如何製作網絡機器人? (或其他)
- 15. JPanel,JFrame,JComponent和JApplet之間的區別
- 16. 在JFrame的JPanel內顯示JComponent
- 17. 靜態庫或其他任何東西
- 18. Drupal:模塊,cck或其他
- 19. 定製Ubuntu或任何其他Linux
- 20. 觀察者模式或其他模式
- 21. 如何從計算函數內部更新其他字段或其他模型?
- 22. 定位模態的JFrame
- 23. 如何強制Jcomponent出現在Java中?
- 24. 如何製作響應模態? CSS
- 25. 如何製作動態ES6模塊?
- 26. 如何製作模態窗口
- 27. 保持JFrame在其他人之上
- 28. Datepicker或其他jquery善良模態窗口內
- 29. 如何使用java訪問其他jframe中的文本框?
- 30. 如何一個JButton從其他類添加到JFrame中