JAVA Swing 圖片按鈕布局

來源:互聯網
上載者:User
package elts.ui;import javax.swing.*;import javax.swing.border.*;import java.awt.*;public class MenuFrame extends JFrame {  public MenuFrame(){  init();  }  private void init(){  setTitle("線上評測");  setSize(600,400);  setResizable(false);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  setLocationRelativeTo(null);  //添加主面板  setContentPane(createContentPane());  }  //建立主面板    private JPanel createContentPane(){  JPanel p=new JPanel(new BorderLayout());  p.add(BorderLayout.NORTH,     createImagePane());  p.add(BorderLayout.CENTER,    createCenterPane());  p.add(BorderLayout.SOUTH,  new JLabel("著作權 盜版必究",JLabel.RIGHT));  return p;  }//建立中間部分的面板private JPanel createCenterPane() {JPanel p=new JPanel(new BorderLayout());info=new JLabel("歡迎***參加考試",JLabel.CENTER);p.add(BorderLayout.NORTH,info);p.add(createMenuPane());return p;}//建立菜單所在的窗格private JPanel createMenuPane() {JPanel p=new JPanel();JButton start=createImageButton("開始","exam.png");p.add(start);JButton result=createImageButton("分數","result.png");p.add(result);JButton message=createImageButton("規則","message.png");p.add(message);JButton exit=createImageButton("離開","exit.png");p.add(exit);return p;}//輔助方法,用於建立映像按鈕private JButton createImageButton(     String text,String image){   ImageIcon icon=new ImageIcon(   getClass().getResource(image));   JButton btn=new JButton(text,icon);   btn.setHorizontalTextPosition(             JButton.CENTER);   btn.setVerticalTextPosition(             JButton.BOTTOM);   return btn;}private JPanel createImagePane() {JPanel p=new JPanel();ImageIcon icon2=new ImageIcon(getClass().getResource("title.png"));p.add(new JLabel(icon2));return p;}//定義私人屬性private JLabel info;//main方法public static void main(String[] args) {MenuFrame menu=new MenuFrame();menu.setVisible(true);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.