所以我繼續我的戰鬥:Creating Java dialogs任務。現在我的JOptionPane打開帶有信封覆蓋的新窗口,但我無法更改此窗口的大小。另外我想讓發件人的數據在左上角,接收者的數據在右下角。我怎樣才能做到這一點? OptionPane本身也存在問題。點擊「確定」後,它會打開屏幕左上角的小窗口。這是什麼以及它出現的原因?
我的代碼:JOptionPane打開另一個JFrame
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 http://img62.imageshack.us/img62/3619/mainscreensnapz001.jpg
alt text http://img24.imageshack.us/img24/8946/mainscreensnapz002.jpg
alt text http://img571.imageshack.us/img571/1621/mainscreensnapz003.jpg
我認爲owca已經給了你一個非常好的推動。你不用這次嘗試一下。此外,包括你有什麼和你想要的東西的截圖可能會有用,所以我們不必複製粘貼你的代碼看到。 – OscarRyz 2010-05-28 01:50:16
剛剛意識到我不能在SO上進行編輯。 [Main](http://img62.imageshack.us/img62/3619/mainscreensnapz001.jpg),[後面會出現什麼](http://img24.imageshack.us/img24/8946/mainscreensnapz002.jpg), [擴大一點](http://img571.imageshack.us/img571/1621/mainscreensnapz003.jpg)。 – 2010-05-28 01:55:31