边框布局不起作用 [英] Border Layout not working

查看:124
本文介绍了边框布局不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设置此边框布局几个小时.我在Java网站上查找了如何使用BorderLayout 但是我还没有得到.

I've been trying to setup this border layout for hours. I've looked up How to Use BorderLayout on the Java website but I still haven't gotten it.

我在下面包括了我的代码以供查看,并使其更易于查看我如何尝试使用BorderLayout函数.

I've included my code below for review and to make it easier to see how I am trying to use the BorderLayout function.

如何在JL3上设置边框布局?

How do I setup a border layout on JL3?

class GameStructure {
    private String []wordList = {"computer","java","activity","alaska","appearance","article",
            "automobile","basket","birthday","canada","central","character","chicken","chosen",
            "cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
            "establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
            "hurried","identity","importance","impossible","invented","italian","journey","lincoln",
            "london","massage","minerals","outer","paint","particles","personal","physical","progress",
            "quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
            "strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
   private JTextField tf;
   private JLabel jl2;

   public void window() {
      ImageIcon ic = new ImageIcon("hangman.png");
      JFrame gameFrame = new JFrame();
      JPanel jp = new JPanel();
      JPanel jpLets = new JPanel();
      JPanel jpBlank = new JPanel();
      JPanel imgPane = new JPanel();
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      panel2.setLayout(new BorderLayout());
      jpLets.setLayout(new BoxLayout(jpLets, BoxLayout.Y_AXIS));
      panel1.setLayout(new BorderLayout());
      panel1.setOpaque(false);//!!
      //jp.setBorder(BorderFactory.createTitledBorder(""));
      tf = new JTextField(1);
      JLabel img = new JLabel(ic, JLabel.CENTER);
      JLabel jl = new JLabel("Enter a letter", JLabel.CENTER);
      jl2 = new JLabel("Letters used:  ", JLabel.CENTER);
      JLabel jl3 = new JLabel("__ ", JLabel.CENTER);
      jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
      tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
      jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
      imgPane.add(img, BorderLayout.CENTER);
      jp.add(jl);
      jp.add(tf);
      jpLets.add(jl2);
      jpBlank.add(jl3);
      //jpMain.add(imgPane, BorderLayout.CENTER);
      panel1.add(jp, BorderLayout.NORTH);
      panel1.add(jpLets, BorderLayout.SOUTH);
      panel1.add(imgPane);
      panel2.add(panel1, BorderLayout.CENTER);
      panel2.add(jpBlank, BorderLayout.PAGE_END);
      gameFrame.setTitle("Hangman");
      gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gameFrame.setIconImage(
      new ImageIcon("Hangman-Game-grey.png").getImage());
      gameFrame.setResizable(false);
      gameFrame.add(panel2);
      gameFrame.setSize(600, 600);
      gameFrame.setLocationRelativeTo(null);
      gameFrame.setVisible(true);

          int j = 0;
          int []length = new int[64];
          for(j = 0; j<64; j++) {
             length[j] = wordList[j].length();//gets length of words in wordList
          }//end for
          int l = 0;
          String line = "";
          //create line first then put into .setText
          for(int m = 0; m<length[l]; m++) {
              line += "__ ";
              l++;
          }//end for
          jl3.setText(line);

          tf.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {//when enter key pressed
              JTextField tf = (JTextField)e.getSource();
              String letter = tf.getText();
              jl2.setText(jl2.getText() + letter + " ");//sets jlabel text to users entered letter
              }//end actionPerformed method
          });
      }//end userInput method
   }

public class GameMain {
   public static void main(String[] args) {
      GameStructure game = new GameStructure();
      game.window();
   }
}

推荐答案

首先,我建议您看一下我昨天链接到您的那个教程,并努力学习:)

First of all, let me commend you on actually looking at that tutorial I linked you yesterday and putting effort into learning :)

这是我看到的几件事.

在这里,您正在尝试为JLabel使用BorderLayout.通常,您希望将LayoutManager与容器(JPanels,JFrames,JDialogs,JApplets)一起使用.摆脱下面的代码.

Here, you're trying to use a BorderLayout for a JLabel. Generally you want to use the LayoutManagers with containers, (JPanels, JFrames, JDialogs, JApplets). Get rid of the code below.

JLabel img = new JLabel(ic, JLabel.CENTER);
img.setLayout( new BorderLayout() );

在这里,还有您所有的jp.add().

Also here, With all your jp.add()'s.

jp.add(img, BorderLayout.CENTER);
jp.add(jl);
jp.add(tf);
jp.add(jl2);
....

我假设您要使用BorderLayout,但是没有为jp面板指定BorderLayout.默认情况下,如果您不指定LayoutManager,JPanel将为您提供FlowLayout

I'm assuming you want to use a BorderLayout, but there was no BorderLayout specified for the jp Panel. By default, if you don't specify a LayoutManager, JPanel will give you a FlowLayout

JPanel jp = new JPanel();

请记住,将BorderLayout分配给容器时,要确保添加到容器中的每个组件都指定了布局"位置.不得多次使用任何头寸.同样,通常应始终使用BorderLayout.CENTER. (例如,如果面板中只有两个组件,请使用BorderLayout.SOUTHBorderLayout.CENTER).

Keep in mind, when you assign a BorderLayout to a container, you want to make sure every component you add to it, has a Layout position specified. No position should be used more than once. Also, a BorderLayout.CENTER generally should always be used. (e.g. if you only have two components in the panel, use BorderLayout.SOUTH, BorderLayout.CENTER).

此外,您应该学习将Layout与多个Panel嵌套在一起以获得所需的结果.这是一个简单的例子.假设您有两个按钮和一个图像标签

Also, you should learn to nest Layout with multiple Panels to get your desired result. Here's a simple example. Say you have two buttons and a Image Label

JLabel label = new JLabel(new ImageIcon(filePath));
JButton jbt1 = new JButton("jbt1");
JButton jbt2 = new JButton("jbt2");

我希望布局看起来像这样

I want the layout to look like this

+--------------------------------+
|                                |
|                                |
|          Image Label           |
|                                |
+----------------+---------------+
|      jbt1      |      jbt2     |
+----------------+---------------+

我要做的第一件事是将按钮添加到单独的JPanel中.然后将标签包裹在JPanel中(不是必需的,但是我喜欢使用面板);

First thing I want to do is add the buttons to a separate JPanel. Then wrap the label in a JPanel (not necessary, but I like using panels);

JPanel buttonsPanel = new JPanel();     <-- FlowLayout (none specified)
buttonsPanel.add(jbt1);
buttonsPanel.add(jbt2);

JPanel labelPanel = new JPanle();       <-- FlowLayout
labelPabel.add(label);

然后,我将把两个面板与另一个带有BorderLayout的面板一起包装

Then I'm going to wrap the two panels with another panel with a BorderLayout

JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(buttonPanel, BorderLayout.SOUTH);
panel1.add(labelPanel, BorderLayout.CENTER);

所以我的最终面板看起来像这样

So my final panel would look like this

            panel1
+--------------------------------+
|                                |
|                                |   BorderLayout.CENTER
|          Image Label           |
|                                |
+----------------+---------------+
|      jbt1      |      jbt2     |   BorderLayout.SOUTH
+----------------+---------------+

假设您想出另一个要添加的面板.您想将此新面板添加到刚创建的面板的左侧.现在,我们只需要使用我们创建的panel1面板并将该面板和新面板包装在另一个面板中

Let's say you came up with another panel you wanted to add. You want to add this new panel to the left of the panel we just created. We only now need to use the panel1 panel we created and wrap that panel and new panel in another panel

JPanel newPanel = new JPanel();

JPanel panel2 new JPanel(new BorderLayout());
panel2.add(newPanel, BorderLayout.WEST);
panel2.add(panel1, BorderLayout.CENTER);

             panel2
+---------------+--------------------------------+
|               |                                |
|               |                                |  
|               |          Image Label           |
|    newPanel   |                                |
|               +----------------+---------------+
|               |      jbt1      |      jbt2     |   
+---------------+----------------+---------------+

查看布局和面板如何.这是处理组件布局的好方法

See how the layout and panel have been. This is great way to handle laying out components

这篇关于边框布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆