JFrame和JPanel里的布局为什么不一样呢?

xzjjy 2006-06-01 07:30:29
从JBuilder 里找了个例子,在原来的基础上又回了个JPanel,界面就变了呢?
这是我改后的代码,大家看看这是什么问题。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Rectangle;

public class Frame1 extends JFrame {

//UI and components
JPanel contentPane;
JPanel panel = new JPanel();
JMenuBar menuBar1 = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
TitledBorder titledBorder1;
JPasswordField jPasswordField1 = new JPasswordField(10);
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JOptionPane jOptionPane1 = new JOptionPane(); /**
* Construct the frame
*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

/**
* Component initialization
*
* @throws Exception exception
*/
private void jbInit() throws Exception {

//initialized content pane
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(new BorderLayout());
this.setSize(new Dimension(350, 300));
this.setTitle("Password Sample");

//initialized menu bar and menu items, as well as menu event listeners
this.setJMenuBar(menuBar1);
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileExit_actionPerformed(e);
}
});

//initialize password field - set echo character to '#' and length
//of field to 10 columns. Add event listener
jPasswordField1.setEchoChar('#');
jPasswordField1.setColumns(10);
jPasswordField1.setBounds(new Rectangle(107, 77, 109, 28));
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jPasswordField1_actionPerformed(e);
}
});

//initialize text field for message
jTextField1.setBackground(Color.red);
jTextField1.setFont(new java.awt.Font("SansSerif", 1, 24));
jTextField1.setBorder(BorderFactory.createRaisedBevelBorder());
jTextField1.setCaretColor(Color.white);
jTextField1.setEditable(false);
jTextField1.setHorizontalAlignment(SwingConstants.CENTER);
jTextField1.setBounds(new Rectangle(6, 145, 329, 101));

//initialize labels
jLabel1.setText("Password is:");
jLabel1.setBounds(new Rectangle(5, 33, 109, 29));
jLabel2.setText("Enter password:");
jLabel2.setBounds(new Rectangle(6, 76, 99, 28));

//initialize button and create event listener
jButton1.setFont(new java.awt.Font("SansSerif", 0, 12));
jButton1.setBorder(BorderFactory.createRaisedBevelBorder());
jButton1.setText("Click for password");
jButton1.setBounds(new Rectangle(106, 22, 142, 44));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
}); //add components
menuFile.add(menuFileExit);
menuBar1.add(menuFile);
panel.add(jTextField1, null);
panel.add(jLabel1, null);
panel.add(jLabel2, null);
panel.add(jPasswordField1, null);
panel.add(jButton1, null);
contentPane.add(panel,BorderLayout.CENTER);
}

/**
* File | Exit action performed
*
* @param e ActionEvent
*/
public void fileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}

/**
* Overridden so we can exit when window is closed
*
* @param e WindowEvent
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
fileExit_actionPerformed(null);
}
}


private boolean isPasswordCorrect(char[] input) {
char[] correctPassword = { 'j', 'b', 'u', 'i', 'l', 'd', 'e', 'r' };
if (input.length != correctPassword.length)
return false;
for (int i = 0; i < input.length; i ++)
if (input[i] != correctPassword[i])
return false;
return true;
}


void jPasswordField1_actionPerformed(ActionEvent e) {
String correctPassword = new String("Good job!");
String incorrectPassword = new String("Opps! Try again!");

JPasswordField input = (JPasswordField)e.getSource();
char[] password = input.getPassword();
if (isPasswordCorrect(password)) {
jTextField1.setText(correctPassword);
JOptionPane.showMessageDialog(jOptionPane1, "Success! You typed the right password.");
}
else {
jTextField1.setText(incorrectPassword);
JOptionPane.showMessageDialog(jOptionPane1, "Invalid password. Try again.",
"Error Message", JOptionPane.ERROR_MESSAGE);
}
}


void jButton1_actionPerformed(ActionEvent e) {
jButton1.setText("jbuilder");
}
}
...全文
355 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
goooooooooo 2006-06-04
  • 打赏
  • 举报
回复
up
beyondone 2006-06-03
  • 打赏
  • 举报
回复
JFrame中的ContentPane的默认布局是BorderLayout,
而JPanel默认布局是FlowLayout。

正解..
UnAgain 2006-06-01
  • 打赏
  • 举报
回复
加上下面的代码
panel.setLayout(new BorderLayout());
即可。
UnAgain 2006-06-01
  • 打赏
  • 举报
回复
JFrame中的ContentPane的默认布局是BorderLayout,
而JPanel默认布局是FlowLayout。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧