gpt4 book ai didi

java - JOptionPane 打开另一个 JFrame

转载 作者:行者123 更新时间:2023-12-01 16:04:27 25 4
gpt4 key购买 nike

所以我继续我的斗争:Creating Java dialogs任务。现在我的 JOptionPane 打开带有信封覆盖的新窗口,但我无法更改此窗口的大小。我还希望发送者的数据位于左上角,接收者的数据位于右下角。我怎样才能做到这一点?OptionPane 本身也存在问题。单击“确定”后,它会在屏幕左上角打开小窗口。这是什么以及为什么会出现?
我的代码:

import java.awt.*;
import java.awt.Font;
import javax.swing.*;

public class Main extends JFrame {

private static JTextField nameField = new JTextField(20);
private static JTextField surnameField = new JTextField();
private static JTextField addr1Field = new JTextField();
private static JTextField addr2Field = new JTextField();
private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" });

public Main(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
getContentPane().add(mainPanel);

JPanel addrPanel = new JPanel(new GridLayout(0, 1));
addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
addrPanel.add(new JLabel("Name"));
addrPanel.add(nameField);
addrPanel.add(new JLabel("Surname"));
addrPanel.add(surnameField);
addrPanel.add(new JLabel("Address 1"));
addrPanel.add(addr1Field);
addrPanel.add(new JLabel("Address 2"));
addrPanel.add(addr2Field);
mainPanel.add(addrPanel);
mainPanel.add(new JLabel(" "));
mainPanel.add(sizes);

String[] buttons = { "OK", "Cancel"};

int c = JOptionPane.showOptionDialog(
null,
mainPanel,
"My Panel",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);

if(c ==0){
new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText()
, addr2Field.getText(), sizes.getSelectedIndex());
}

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public static void main(String[] args) {
new Main();
}
}

class Envelope extends JFrame {

private final int SMALL=0;
private final int MEDIUM=1;
private final int LARGE=2;
private final int XLARGE=3;

public Envelope(String n, String s, String a1, String a2, int i){
Container content = getContentPane();

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(new JLabel("John Doe"));
mainPanel.add(new JLabel("FooBar str 14"));
mainPanel.add(new JLabel("Newark, 45-99"));

JPanel dataPanel = new JPanel();
dataPanel.setFont(new Font("sansserif", Font.PLAIN, 32)); //set size from i
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setBackground(Color.ORANGE);
mainPanel.add(new JLabel("Mr "+n+" "+s));
mainPanel.add(new JLabel(a1));
mainPanel.add(new JLabel(a2));

content.setSize(450, 600);
content.setBackground(Color.ORANGE);
content.add(mainPanel, BorderLayout.NORTH);
content.add(dataPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}

Main

alt text

alt text

最佳答案

Now my JOptionPane opens new window with envelope overfiew, but I can't change size of this window.

那是因为您使用的是 JOptionPane,托盘使用的是 JDialog 或 JFrame

Also I wanted to have sender's data in upper left corner, and receiver's data in bottom right. How can I achieve that ?

您认为如何才能做到这一点?

There is also issue with OptionPane itself. After I click 'OK' it opens small window in the upper left corner of the screen. What is this and why it's appearing ?

这是你的代码,你应该更清楚。这就是“new Envelope...”中的代码

关于java - JOptionPane 打开另一个 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2926182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com