2016-02-26 23 views
1

我希望2016年标签酒店管理系统版权出现在底部的MatteBorder以下,并且居中。我试过copyrightL.setVerticalAlignment(JLabel.BOTTOM);,但它似乎并没有工作。Java Swing-在GridBagLayout中的单个底部边框下添加标签

这里是我的代码:

public class Admin_hs extends JFrame { 

JButton bking_btn= new JButton("Bookings"); 
JButton fd_btn= new JButton("Financial Data"); 
JButton ctm_btn= new JButton("Customers"); 
JButton room_btn= new JButton("Rooms"); 
JButton adc_btn= new JButton("Additional Costs"); 
JButton endb_btn= new JButton("Ending Bookings"); 

//Images 
JLabel bking_img= new JLabel(); 
JLabel fd_img= new JLabel(); 
JLabel ctm_img= new JLabel(); 
JLabel room_img= new JLabel(); 
JLabel adc_img= new JLabel(); 
JLabel endb_img= new JLabel(); 
JLabel copyrightL = new JLabel("Hotel Management System \u00a9 2016"); 

///Panels 

JPanel pnl1= new JPanel(); 
JPanel pnl= new JPanel((new FlowLayout(FlowLayout.LEFT))); 

///Constructors 

public Admin_hs(){ 
    this.setTitle("Welcome Admin!"); 
    this.setLayout(new GridBagLayout()); 

    ///Setting a layout 

    pnl1.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridwidth= GridBagConstraints.REMAINDER; 
    gbc.fill= gbc.HORIZONTAL; 

    gbc.weightx = 1;      // ADD THIS LINE so it uses all the existing window size in x direction 
    pnl1.add(pnl);       // ADD THIS LINE 


    pnl.setLayout(new GridBagLayout()); 

    GridBagConstraints gc= new GridBagConstraints(); 

    ///First Column of Grid 


    gc.insets = new Insets(6, 6, 6, 6); 
    gc.anchor = GridBagConstraints.WEST; 
    gc.weightx = 0.5; 
    gc.weighty = 0.5; 

    gc.gridx = 0; 
    gc.gridy = 0; 

    pnl.add(bking_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 1; 
    pnl.add(fd_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 2; 
    pnl.add(ctm_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 3; 
    pnl.add(room_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 4; 
    pnl.add(adc_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 5; 
    pnl.add(endb_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 8; 
    pnl.add(copyrightL, gc); 


    /////second column of grid 



    gc.anchor = GridBagConstraints.WEST; 
    gc.gridx = 1; 
    gc.gridy = 0; 
    bking_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/booking.jpg")); 
    pnl.add(bking_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 1; 
    fd_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/fd.jpg")); 
    pnl.add(fd_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 2; 
    ctm_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/guest.jpg")); 
    pnl.add(ctm_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 3; 
    room_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/room.jpg")); 
    pnl.add(room_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 4; 
    adc_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/adc.jpg")); 
    pnl.add(adc_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 5; 
    endb_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/endb.png")); 
    pnl.add(endb_img, gc); 

    //copyrightL.setHorizontalAlignment(JLabel.CENTER); 
    //copyrightL.setVerticalAlignment(JLabel.BOTTOM); 
    //pnl.add(copyrightL); 

    pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)); 

    this.add(pnl1,gbc); 


    } 

} 

主类

public class Admin_main { 

public static void main(String[] args) { 

    Admin_hs adm= new Admin_hs(); 

    adm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    adm.pack(); 
    adm.setVisible(true); 
    adm.setSize(780,520); 

    } 

} 

这是它目前的样子:

enter image description here


编辑:

它以这种方式为我工作:

public class Admin_hs extends JFrame { 

JButton bking_btn= new JButton("Bookings"); 
JButton fd_btn= new JButton("Financial Data"); 
JButton ctm_btn= new JButton("Customers"); 
JButton room_btn= new JButton("Rooms"); 
JButton adc_btn= new JButton("Additional Costs"); 
JButton endb_btn= new JButton("Ending Bookings"); 

//Images 
JLabel bking_img= new JLabel(); 
JLabel fd_img= new JLabel(); 
JLabel ctm_img= new JLabel(); 
JLabel room_img= new JLabel(); 
JLabel adc_img= new JLabel(); 
JLabel endb_img= new JLabel(); 
JLabel copyrightL = new JLabel("Hotel Management System \u00a9 2016"); 


///Panels 

JPanel pnl1= new JPanel(); 
JPanel pnl2= new JPanel(new GridBagLayout()); 

///Constructors 

public Admin_hs(){ 
    this.setTitle("Welcome Admin!"); 
    this.setLayout(new GridBagLayout()); 

    ///Setting a layout 



    pnl1.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.fill= gbc.HORIZONTAL; 


    GridBagConstraints gc= new GridBagConstraints(); 

    ///First Column of Grid 


    gc.insets = new Insets(6, 6, 6, 6); 
    gc.anchor = GridBagConstraints.WEST; 
    gc.weightx = 0.5; 
    gc.weighty = 0.5; 

    gc.gridx = 0; 
    gc.gridy = 0; 

    pnl1.add(bking_btn, gc); 


    gc.gridx = 0; 
    gc.gridy = 1; 
    pnl1.add(fd_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 2; 
    pnl1.add(ctm_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 3; 
    pnl1.add(room_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 4; 
    pnl1.add(adc_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 5; 
    pnl1.add(endb_btn, gc); 

    gc.gridx = 0; 
    gc.gridy = 8; 
    pnl1.add(copyrightL, gc); 


    /////second column of grid 



    gc.anchor = GridBagConstraints.WEST; 
    gc.gridx = 1; 
    gc.gridy = 0; 
    bking_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/booking.jpg")); 
    pnl1.add(bking_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 1; 
    fd_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/fd.jpg")); 
    pnl1.add(fd_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 2; 
    ctm_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/guest.jpg")); 
    pnl1.add(ctm_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 3; 
    room_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/room.jpg")); 
    pnl1.add(room_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 4; 
    adc_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/adc.jpg")); 
    pnl1.add(adc_img, gc); 

    gc.gridx = 1; 
    gc.gridy = 5; 
    endb_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/endb.png")); 
    pnl1.add(endb_img, gc); 


    pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)); 

    gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 2; 
     pnl2.add(copyrightL, gbc); 

     gbc.gridwidth = 1; 
     gbc.weightx = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     add(pnl1, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     add(pnl2, gbc); 

    } 

}

这是现在的样子:

enter image description here

按钮具有不知何故向左移动。这对我来说很好,因为我出于某种原因必须将它们移动到左侧。我怎样才能让他们相应的图像到左边?我的意思是,我怎样才能让图像更靠近左边的按钮?

+0

所以期望的输出是具有与著作权t语句在灰线下。正确吗? – user3437460

+0

我的回答是你在做什么或者你需要任何额外的帮助? – Dan

+0

@ user3437460是的,这就是所需的输出 – Tia

回答

2

我认为这样做最简单的方法是使用类似的代码来此

import javax.swing.*; 
import java.awt.*; 

public class Test extends JFrame { 
    private JPanel testPanel = new JPanel(new GridBagLayout()), testPanel2 = new JPanel(new GridBagLayout()); 
    private GridBagConstraints c = new GridBagConstraints(); 
    private JLabel label1 = new JLabel("Something"), label2 = new JLabel("Something Else"); 

    Test() { 
     super("Example Frame"); 
     setLayout(new GridBagLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     buildGUI(); 

     c.weightx = 1.0; 
     c.gridx = 0; 
     c.gridy = 0; 
     add(testPanel, c); 

     c.gridx = 0; 
     c.gridy = 1; 
     add(testPanel2, c); 

     pack(); 
     setVisible(true); 
    } 

    private void buildGUI() { 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 
     testPanel.add(label1, c); 

     testPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)); 

     c.gridx = 0; 
     c.gridy = 0; 
     testPanel2.add(label2, c); 
    } 

    public static void main(String[] args) { 
     new Test(); 
    } 
} 

在这段代码中,有两个JPanels,每个上面有一个JLabel。第一个面板底部有一个边框,第二个面板放置在底部。您可以通过在JFrame上设置布局来实现这一点您可以采用这个想法来处理您的代码。

用你的代码,它看起来像这样。

Admin_main

import javax.swing.*; 

public class Admin_main { 
    public static void main(String[] args) { 

     Admin_hs adm= new Admin_hs(); 

     adm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     adm.pack(); 
     adm.setVisible(true); 
    } 
} 

Admin_hs

import javax.swing.*; 
import java.awt.*; 

public class Admin_hs extends JFrame { 
    JButton bking_btn= new JButton("Bookings"); 
    JButton fd_btn= new JButton("Financial Data"); 
    JButton ctm_btn= new JButton("Customers"); 
    JButton room_btn= new JButton("Rooms"); 
    JButton adc_btn= new JButton("Additional Costs"); 
    JButton endb_btn= new JButton("Ending Bookings"); 

    //Images 
    JLabel bking_img= new JLabel(); 
    JLabel fd_img= new JLabel(); 
    JLabel ctm_img= new JLabel(); 
    JLabel room_img= new JLabel(); 
    JLabel adc_img= new JLabel(); 
    JLabel endb_img= new JLabel(); 
    JLabel copyrightL = new JLabel("Hotel Management System \u00a9 2016"); 

    ///Panels 

    JPanel pnl1 = new JPanel(new GridBagLayout()), pnl2 = new JPanel(new GridBagLayout()); 

    ///Constructors 

    public Admin_hs(){ 
     setTitle("Welcome Admin!"); 
     setLayout(new GridBagLayout()); 

     ///Setting a layout 

     pnl1.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill= GridBagConstraints.HORIZONTAL; 

     gbc.insets = new Insets(6, 6, 6, 6); 
     gbc.weightx = 1; 
     gbc.weighty = 1; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     pnl1.add(bking_btn, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     pnl1.add(fd_btn, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 2; 
     pnl1.add(ctm_btn, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 3; 
     pnl1.add(room_btn, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 4; 
     pnl1.add(adc_btn, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 5; 
     pnl1.add(endb_btn, gbc); 


     /////second column of grid 
     gbc.weightx = 0; 
     gbc.fill= GridBagConstraints.BOTH; 

     ImageIcon icon = new ImageIcon("Smiley.gif"); 
     Image image2 = icon.getImage().getScaledInstance(50, 50, 0); 
     ImageIcon icon2 = new ImageIcon(image2); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     bking_img.setIcon(icon2); 
     pnl1.add(bking_img, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     fd_img.setIcon(icon2); 
     pnl1.add(fd_img, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 2; 
     ctm_img.setIcon(icon2); 
     pnl1.add(ctm_img, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 3; 
     room_img.setIcon(icon2); 
     pnl1.add(room_img, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 4; 
     adc_img.setIcon(icon2); 
     pnl1.add(adc_img, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 5; 
     endb_img.setIcon(icon2); 
     pnl1.add(endb_img, gbc); 

     pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)); 

     gbc.insets = new Insets(0, 0, 0, 0); 

     gbc.gridwidth = 1; 
     gbc.weightx = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     add(pnl1, gbc); 

     gbc.fill= GridBagConstraints.HORIZONTAL; 

     gbc.weightx = 0; 
     gbc.weighty = 0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 2; 
     pnl2.add(copyrightL, gbc); 

     gbc.weightx = 0; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     add(pnl2, gbc); 
    } 
} 

这产生本

enter image description here