我正在尝试使用简单的Swing应用程序。我在面板中垂直添加了6个按钮,但是在执行程序时并不是所有都可见。底部按钮未显示。我添加了滚动窗格功能,但它没有发生。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class Frame6 extends JFrame {
private BorderLayout layoutMain = new BorderLayout();
private JPanel panelCenter = new JPanel();
// private JScrollPane panel1 = new JScrollPane(panel);
private JMenuBar menuBar = new JMenuBar();
private JMenu menuFile = new JMenu();
private JMenuItem menuFileExit = new JMenuItem();
private JToolBar toolBar = new JToolBar();
private JButton buttonOpen = new JButton();
private JButton buttonClose = new JButton();
private JButton buttonHelp = new JButton();
private ImageIcon imageOpen = new ImageIcon(Frame6.class.getResource("openfile.gif"));
private ImageIcon imageClose = new ImageIcon(Frame6.class.getResource("closefile.gif"));
private ImageIcon imageHelp = new ImageIcon(Frame6.class.getResource("help.gif"));
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private JButton jButton4 = new JButton();
private JButton jButton5 = new JButton();
private JButton jButton6 = new JButton();
public Frame6() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setVisible(true);
this.setJMenuBar( menuBar );
this.getContentPane().setLayout( layoutMain );
panelCenter.setLayout( null );
this.setSize(new Dimension(401, 1100));
menuFile.setText( "File" );
menuFileExit.setText( "Exit" );
menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
buttonOpen.setToolTipText( "Open File" );
buttonOpen.setIcon( imageOpen );
buttonClose.setToolTipText( "Close File" );
buttonClose.setIcon( imageClose );
buttonHelp.setToolTipText( "About" );
buttonHelp.setIcon( imageHelp );
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(45, 75, 160, 20));
jButton2.setText("jButton2");
jButton2.setBounds(new Rectangle(40, 200, 185, 20));
jButton3.setText("jButton3");
jButton3.setBounds(new Rectangle(45, 390, 210, 20));
jButton4.setText("jButton4");
jButton4.setBounds(new Rectangle(35, 590, 175, 20));
jButton5.setText("jButton5");
jButton5.setBounds(new Rectangle(30, 755, 200, 20));
jButton6.setText("jButton6");
jButton6.setBounds(new Rectangle(55, 945, 190, 20));
menuFile.add( menuFileExit );
menuBar.add( menuFile );
toolBar.add( buttonOpen );
toolBar.add( buttonClose );
toolBar.add( buttonHelp );
this.getContentPane().add( toolBar, BorderLayout.NORTH );
panelCenter.add(jButton6, null);
panelCenter.add(jButton5, null);
panelCenter.add(jButton4, null);
panelCenter.add(jButton3, null);
panelCenter.add(jButton2, null);
panelCenter.add(jButton1, null);
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
}
public static void main(String[] args) {
new Frame6();
}
void fileExit_ActionPerformed(ActionEvent e) {
System.exit(0);
}
}