62,614
社区成员
发帖
与我相关
我的任务
分享
private void setUpUIComponent()
{
setSize(400, 300);
setResizable(false);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuNew = new JMenuItem("New");
menuNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK));
menuOpen = new JMenuItem("Open PVD...");
menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuBuild = new JMenuItem("Build PVD...");
menuBuild.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,
InputEvent.CTRL_MASK));
menuClose = new JMenuItem("Quit");
menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
InputEvent.CTRL_MASK));
fileMenu.add(menuNew);
fileMenu.add(menuOpen);
fileMenu.add(menuBuild);
fileMenu.addSeparator();
fileMenu.add(menuClose);
JMenu aboutMenu = new JMenu("About");
menuAbout = new JMenuItem("About PVD Builder");
aboutMenu.add(menuAbout);
menuBar.add(fileMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
Container contentPane = getContentPane();
setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
lblSummary = new JLabel("Summary:");
lblDescription = new JLabel("Description");
contentPane.add(lblSummary);
txtSummary = new JTextArea();
txtSummary.setFont(new Font("微软雅黑", Font.PLAIN, 16));
txtSummary.setLineWrap(true);
JScrollPane panel = new JScrollPane(txtSummary,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(panel);
txtDescription = new JTextArea();
txtDescription.setFont(new Font("微软雅黑", Font.PLAIN, 16));
txtDescription.setLineWrap(true);
JScrollPane panel2 = new JScrollPane(txtDescription,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(lblDescription);
contentPane.add(panel2);
stateBar = new JLabel("New");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(BorderFactory.createEtchedBorder());
contentPane.add(stateBar);
}
private void setUpInsertListener()
{
txtSummary.setDocument(new PlainDocument()
{
public void insertString(int offset,String str,AttributeSet as)
throws BadLocationException{
if(this.getLength()+str.length() >= 26)
{
java.awt.Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "Summary should less than 20 characters!",
"Error",JOptionPane.WARNING_MESSAGE);
}
else
super.insertString(offset,str,as);
}
});
}
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestLayout extends JFrame{
public TestLayout() {
this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
this.setSize(new Dimension(600,400));
JLabel label1 = new JLabel("1");
this.add(label1);
JLabel label2 = new JLabel("2");
this.add(label2);
JLabel label3 = new JLabel("3");
this.add(label3);
JLabel label4 = new JLabel("4");
this.add(label4);
this.setVisible(true);
}
public static void main(String[] args){
TestLayout tl = new TestLayout();
}
}