2015-04-01 90 views
-1

我试图用java创建一个GUI。我的gui会很简单。你可以从这里看到我想要的:http://sketchtoy.com/64839370使用边框布局创建GUI Swing

为了做到这一点,我决定使用网络上建议的BorderLayout。我有两个Jpanel对象,并将它们放入jFrame,其布局是borderlayout。你可以看到我下面的简化代码:

private Display display= new Display(); // Display extends JPanel 
public Simulation() 
    { 
     super(); 
     // frame settings 
     setTitle("Label of JFrame "); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setBounds(100,100,1094,560);  
     contentPane=this.getContentPane(); 
     setResizable(false); 

     contentPane.setLayout(new BorderLayout()); 

     try { 
      LeftPanelLogo=ImageIO.read(new File("logo.png")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // generate left panel (information panel) 
     leftPanel=new JPanel(){ 
      @Override 
      public void paintComponent(Graphics g) 
      { 
       super.paintComponent(g); 
       Graphics2D g2d=(Graphics2D)g; 
       g2d.drawImage(LeftPanelLogo, 10, 250, null); 
      } 
     }; 
     //leftPanel.setLayout(null); 

     // add panels to contentPane 


     leftPanel.setBackground(Color.WHITE); 
     display.setBackground(Color.BLACK); 

     contentPane.add(leftPanel,BorderLayout.WEST); 
     contentPane.add(display,BorderLayout.CENTER); 
} 

在显示类的构造函数我只有下面的代码:

try 
     { 
      bgPicture = ImageIO.read(new File("bg.jpg")); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

当我运行的代码,我看到几乎所有的屏幕满足用面板在中间,我看不到左面板,(换句话说,因为我把显示面板的背景设置为黑色,所有的屏幕都是黑色的)

那么,我该如何解决它?

+0

'leftPanel.setLayout(NULL);'Java的图形用户界面有不同的OS”,屏幕大小,屏幕分辨率等。这样的工作,他们是不利于像素的完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。使用布局也可能是解决这个问题的方法。 – 2015-04-01 13:40:39

+0

@AndrewThompson感谢您的信息,但尽管我把它发表评论,它并没有改变任何东西 – 2015-04-01 13:41:50

+0

1)为了更好地帮助更快,发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整可验证例子)或[SSCCE](http://www.sscce.org/)(简短的,独立的,正确的例子)。 2)获取图像的一种方法是通过[本问答](http://stackoverflow.com/q/19209650/418556)中的图像进行热链接。 3)请学习常用的Java命名规则(命名约定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')并且一致地使用它。 – 2015-04-01 13:42:52

回答

1

enter image description here

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class LogoLayout { 

    private JComponent ui = null; 

    LogoLayout() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     BufferedImage logo = new BufferedImage(
       276,560,BufferedImage.TYPE_INT_RGB); 

     /* All that's needed */ 
     ui.add(new JLabel(new ImageIcon(logo)), BorderLayout.LINE_START); 
     ui.add(new JTextArea("Display", 3, 44)); 
     /* All that's needed */ 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       LogoLayout o = new LogoLayout(); 

       JFrame f = new JFrame("Logo Layout"); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

@Andrew_Thompson对不起,这是我的错,系统故障 – 2015-04-01 13:58:12

+0

请[接受答案](http://meta.stackexchange.com/a/5235/155831)如果它帮助解决问题。 – 2015-04-01 13:58:47

+0

@Andrew_Thompson我认为你误解了我,在我的问题中,我问了2个面板,第一个面板包含一个徽标和一些文本,第二个面板(显示面板)我将在您的解决方案中放置一个背景图像,我无法做到这一点 – 2015-04-01 14:11:05