[英]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: 如您最近的评论之一所示这里是一些注释/解释:
addComponentsToPane()
is never called, thus you never create any BoxLayout
objects addComponentsToPane()
不会被调用,因此您永远不会创建任何BoxLayout
对象 Window
. Window
。 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. BorderLayout
by default. 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.CENTER
开头)。 See documentation of the JFrame class where it says the default is BorderLayout. JPanel
, when created with the default constructor has a FlowLayout
by default. JPanel
默认情况下具有FlowLayout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.