简体   繁体   English

在Java Swing中挣扎于BoxLayout

[英]Struggling with BoxLayout in Java Swing

Whats up guys I'm struggling to understand how to implement BoxLayout or any layout in java swing. 怎么了,我正在努力了解如何在Java swing中实现BoxLayout或任何布局。 I have been looking at tutorials on oracle and others but i just can't get it to work. 我一直在看有关oracle等的教程,但我只是无法使它正常工作。 This is for an assignment in college so I would appreciate not giving me the solution straight up but maybe just point me in the right direction. 这是大学里的一项工作,所以我不希望不给我直接的解决方案,而只是给我指出正确的方向。 I think the problem is my code is different to what is in the tutorials so I'm not sure what goes where. 我认为问题是我的代码与教程中的代码不同,所以我不确定该去哪里。

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.BoxLayout;


class Window extends JFrame implements ActionListener 
{


    JPanel panel = new JPanel();
    JTextField input = new JTextField(10);
    JButton but1 = new JButton ("Convert");
    JLabel label = new JLabel();
    JTextArea output = new JTextArea(1, 20);



    public static void main(String[] args)
        {
            Window gui = new Window();
            String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        }


    public Window()
        {
            super("Swing Window");
            setSize(500, 200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            panel.add(input);
            but1.addActionListener(this);
            add(panel);
            panel.add(output);
            label.setText ("please enter celsius to be converted to Fahrenheit");
            panel.add(but1);
            panel.add(label);
            setVisible(true);   
        }

    public void actionPerformed(ActionEvent event)
    {
        String inputStr = input.getText();
        inputStr = inputStr.trim();
        double input = Double.parseDouble(inputStr);
        double fahrenheit = input * 1.8 + 32;


        if (event.getSource() == but1)
        {
            output.setText("Here is degrees celsius " + input + " converted `to Fahrenheit: " + fahrenheit);`
        }
    }


}

There is the executable code. 有可执行代码。

I skimmed your code, but did not execute it. 我浏览了您的代码,但没有执行。 As others already mentioned in the comments, it is helpful for you to describe what the program does and to describe what you expect/want it to do. 正如其他人在评论中已经提到的那样,它有助于您描述程序的功能并描述您希望/希望执行的操作。 Otherwise we are just guessing as to what is wrong and what would be correct. 否则,我们只是在猜测什么是错误的,什么是正确的。

From my reading of your code, I think you see nothing displayed. 通过阅读您的代码,我认为您什么都没显示。 Correction: you did call add() ; 更正:您确实调用过add() as indicated in one of your more recent comments Here are some notes/explanations: 如您最近的评论之一所示这里是一些注释/解释:

  • Your method addComponentsToPane() is never called, thus you never create any BoxLayout objects 您的方法addComponentsToPane()不会被调用,因此您永远不会创建任何BoxLayout对象
  • Recommendation: variable names begin with lowercase, and don't name a variable the same as a class; 建议:变量名以小写开头,不要将变量命名为与类相同的名称。 it easily creates confusion when reading the code. 在阅读代码时,很容易造成混乱。 Thus don't name the argument Window . 因此,请勿将参数命名为Window
  • Your method addComponentsToPane() , if it were called, creates a Layout object and sets it on the component passed to it but does not actually add any components. 您的方法addComponentsToPane() (如果已调用)将创建一个Layout对象,并将其设置在传递给它的组件上,但实际上不会添加任何组件。 The name is thus misleading. 因此,该名称具有误导性。
  • A JFrame's content pane has a BorderLayout by default. JFrame的内容窗格默认情况下具有BorderLayout When components are added without any additional constraints, the BorderLayout chooses to place the components in a certain order (starting with BorderLayout.CENTER ). 当添加组件时没有任何其他约束时,BorderLayout选择以一定顺序放置组件(以BorderLayout.CENTER开头)。 See documentation of the JFrame class where it says the default is BorderLayout. 请参阅JFrame类的文档,其中说默认值为BorderLayout。
  • A JPanel , when created with the default constructor has a FlowLayout by default. 使用默认构造函数创建的JPanel默认情况下具有FlowLayout
  • Since your program never changed the panel's layout manager, this is what gives you the left-to-right flow that you observed. 由于您的程序从未更改面板的布局管理器,因此可以为您提供从左到右的观察流程。
  • You want to set the panel's layout to a vertical BoxLayout before you add components to it 您想要在将面板添加到组件之前将面板的布局设置为垂直的BoxLayout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM