JLabel、网格布局
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame
{
private JFrame window = new JFrame();
private JButton but[] = new JButton[9];
public Gui()
{
window.setSize(300,400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(4, 3));
JLabel txt = new JLabel("Will you dare?", JLabel.CENTER);
txt.setLayout(new GridLayout(1, 1));
txt.setHorizontalTextPosition(JLabel.CENTER);
txt.setFont(new Font("Serif", Font.PLAIN, 21));
window.add(txt);
for(int i = 0; i < 9; i++)
{
but[i] = new JButton();
window.add(but[i]);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}
我是Java菜鸟,所以,你能告诉我,为什么“你敢吗”文本没有出现在一行中?你能告诉我你是如何找到解决方案的吗,这样下次我也可以自己做。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame
{
private JFrame window = new JFrame();
private JButton but[] = new JButton[9];
public Gui()
{
window.setSize(300,400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(4, 3));
JLabel txt = new JLabel("Will you dare?", JLabel.CENTER);
txt.setLayout(new GridLayout(1, 1));
txt.setHorizontalTextPosition(JLabel.CENTER);
txt.setFont(new Font("Serif", Font.PLAIN, 21));
window.add(txt);
for(int i = 0; i < 9; i++)
{
but[i] = new JButton();
window.add(but[i]);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}
I am Java noob, so, can you tell me, why "Will you dare" text doesn't appear in one line? Can you tell me how you found the solution, so other time I also will be able to do that by myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里大胆猜测,但我想你想要类似的东西:
Wild guess here, but I'm thinking you want something like:
您的代码中有两个注意事项:
您使用以下表达式添加组件
这两个调用都是快捷方式
,并且 JFrame 的内容窗格默认具有 BorderLayout。默认情况下,如果添加时没有指定其他区域/约束,此布局将在中心区域添加组件。因此,您只需将组件一个一个地放在另一个之上,只保留最后一个。
您应该“剪切”(糟糕的翻译,切片?)更多的 GUI,在中心添加 JLabel,并在南边添加另一个 JPanel,并在其中使用流布局并添加按钮。
这是使用我提到的布局的一个好的开始,非常标准。
另外您使用 JLabel 作为容器。几乎所有的 swing 组件都继承自
java.awt.Container
,swing 使用它来提供复合组件,但实际上,您永远不应该将它们用作容器,即不要将其他组件放入其中或给它们一个容器。布局。你这样做,给它一个布局,所以删除这一行。使用swing组件作为组件,使用JPanel(或JTabbedPane等)作为容器。
问候,
史蒂芬
顺便说一句,永远不要对值进行硬编码,而应使用常量。没有 9,但有一个 BUTTON_COUNT = 9 静态成员。
你可以考虑买一本java书。我曾经向我的学生推荐Deitel和Deitel。
There are two caveats in your code :
you use the following expression to add components
both calls are shortcuts to
and the content pane of a JFrame has BorderLayout by default. And by default, this layout will add components in the center area if no other area/constraint is specified when adding. So you just put components one above the other, only the last will remain.
You should "cut"(poor translation, slice ?) more your GUI, adding your JLabel at center, and adding another JPanel on south, and inside it, use a flow layout and add your buttons.
Here is a good start for using the layout I mentionned, which are very standard.
Also you use a JLabel as a container. Almost all swing components inherit from
java.awt.Container
, this is used by swing to provide compound components but actually, you should never use them as container, i.e. not putting other components inside them or giving them a layout. You do that, giving it a layout at lineso remove this line. Use swing components as components and JPanel (or JTabbedPane, etc.) as containers.
Regards,
Stéphane
btw, never hardcode values, use constants. No 9 but a BUTTON_COUNT = 9 static member.
You could consider buying a java book. I used to recommend Deitel and Deitel to my students.
这里的问题非常简单:您正在向标签“txt”添加布局。
您是说标签“txt”应该首先占据第一行JFrame 的列。第一行第一列将产生一个框。当您创建这样的框时,您需要确保该框足够宽以使文本完全适合单行。在这种情况下,您的文本只是跳转到下一行,因为您创建的“框”中没有足够的空间来容纳整个标签以适应单行。
问题修复:
txt.setLayout(new GridLayout(1, 1));
OR
txt.setLayout(new GridLayout(1, 1));
totxt.setLayout(new GridLayout(1,2));
您还有我注意到的另一个问题
private JButton but[] = new JButton[9];
:JLabel txt = new JLabel("你敢吗?");//去掉 'JLabel .CENTER'
应该可以了。(我不认为我错过了任何东西)。 :)
The problem here is quite simple: You are adding a layout to your label "txt".
You are saying that the label "txt" should take up the first row, first column of your JFrame. The first row, first column would result in a box. When you create a box like that you need to make sure the box is wide enough for your text to completely fit on a single line. In this case your text is simply jumping to the next line because there isn't enough room in that "box" you created for the entire label to fit on a single line.
Fixes to the problem:
txt.setLayout(new GridLayout(1, 1));
OR
txt.setLayout(new GridLayout(1, 1));
totxt.setLayout(new GridLayout(1,2));
You have one other problem that i noticed:
private JButton but[] = new JButton[9];
:JLabel txt = new JLabel("Will you dare?");//Removing the 'JLabel.CENTER'
That should do it.(i don't think i missed anything). :)