//Test.java 主窗体
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("关于读者");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("关于作者");
mnNewMenu.add(mntmNewMenuItem);
mntmNewMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new About_Author().setVisible(true);;
}
});
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
//About_Author.java 作者页面信息窗体
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class About_Author extends JFrame{
private final JPanel contentPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
About_Author dialog = new About_Author();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public About_Author() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel label = new JLabel("QQ");
label.setBounds(160, 10, 54, 15);
contentPanel.add(label);
}
{
JLabel lblNewLabel = new JLabel("手机");
lblNewLabel.setBounds(160, 59, 54, 15);
contentPanel.add(lblNewLabel);
}
JLabel label = new JLabel("作者");
label.setBounds(160, 107, 54, 15);
contentPanel.add(label);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
追问你这个到是挺靠谱的,
我看有人有这个append,好像简单的多,你知道怎么用吗?
// contentPane = new JPanel();
。。。
// setContentPane(contentPane);
这段代码,感觉没有用啊,我注释掉后,没有任何影响
追答?append()函数?连接字符串?
// contentPane = new JPanel();
。。。
// setContentPane(contentPane);
这个创建项目时系统自动添加的。创建一个面板用来添加其它控件,然后设置为窗口面板
如果你要往窗口中添加控件,就要用到它了- contentPane.add();
追问是的,我想用append添加字符串,你能帮忙写点什么吗?
我知道怎么加,但是加不上去,拜托了
追答加在哪个位置,加什么,说清楚点啊