JApplet中添加JButton控件,JButton填满整个屏幕
我对 Java Applet 编程和一般 Java 很陌生,但我非常擅长 C# 和 C++。不管怎样,我正在用 JTextField、JButton 和 JLabel 制作一个简单的计算器。但是 JButton 和 JTextField 占据了整个屏幕,最奇怪的是我设置了大小,但它没有设置大小。那么我做错了什么?这是下面的代码,所以有人可以帮助我;我将不胜感激任何答案。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
*
* @author Danny
*/
public class Applet extends JApplet {
private JButton button1;
private JTextField textBox1;
@Override
public void paint(Graphics g)
{
g.setFont(new Font("Courier", Font.PLAIN, 12));
g.drawString("Enter a number: ", 36, 36);
return;
}
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void init() {
button1 = new JButton("Calculate");
textBox1 = new JTextField("number");
textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
textBox1.setSize(100, 36);
button1.setSize(100, 36);
textBox1.setLocation(new Point(36, 76));
button1.setLocation(new Point(36, 70));
Container c = getContentPane();
c.add(button1);
c.add(textBox1);
c.setBackground(Color.gray);
}
// TODO overwrite start(), stop() and destroy() methods
}
I am new to Java Applet programming and Java in general but I am very good in C# and C++. Anyway I am making a simple calculator with a JTextField, JButton and JLabel. But the JButton and JTextField take up the whole screen and the strangest thing is I set the size and it is not setting the size. So what am I doing wrong? Here is the code below so somebody can please help me; I will appreciate any answers.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
*
* @author Danny
*/
public class Applet extends JApplet {
private JButton button1;
private JTextField textBox1;
@Override
public void paint(Graphics g)
{
g.setFont(new Font("Courier", Font.PLAIN, 12));
g.drawString("Enter a number: ", 36, 36);
return;
}
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void init() {
button1 = new JButton("Calculate");
textBox1 = new JTextField("number");
textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
textBox1.setSize(100, 36);
button1.setSize(100, 36);
textBox1.setLocation(new Point(36, 76));
button1.setLocation(new Point(36, 70));
Container c = getContentPane();
c.add(button1);
c.add(textBox1);
c.setBackground(Color.gray);
}
// TODO overwrite start(), stop() and destroy() methods
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在这里考虑布局。 JApplet 的 contentPane 默认使用 BorderLayout,如果您将组件添加到 BorderLayout,而没有第二个参数告诉其添加位置,则它将添加 BorderLayout.CENTER 并填充尽可能多的空间。解决这个问题的关键是尽可能多地了解布局管理器,并利用这些信息来发挥自己的优势。您可以在此处找到此信息:布局管理器视觉指南 。
请注意,我们经常嵌套 JPanel,每个 JPanel 使用自己的编码器友好的布局管理器,以实现复杂但易于管理的布局。
例如,
另请注意,大多数布局管理器通常会忽略 setSize。相反,preferredSize 通常受到尊重,但应避免设置此值,因为您会希望组件本身根据其内容或其他属性(例如 JTextArea 的行和列或 JTextField 的列或字符串文本)设置其 PreferredSize对于 JLabel)。
You need to take layouts into consideration here. A JApplet's contentPane uses BorderLayout by default, and if you add a component to BorderLayout without a second parameter telling it where to add it, it will be added BorderLayout.CENTER and will fill as much space as possible. The key to solving this is to learn as much as possible about layout managers and use this information to your advantage. You can find this information here: Visual Guide to the Layout Managers.
Note that often we nest JPanels, each using its own coder-friendly layout manager, in order to achieve complex but easy to manage layouts.
e.g.,
Note also that setSize is usually ignored by most layout managers. Instead preferredSize is usually honored, but setting this should be avoided as instead you'll want the components themselves to set their preferredSize based on their contents or other properties (such as rows and columns for a JTextArea or columns for a JTextField, or String text for a JLabel).
掌握 Swing 类的功能可能非常棘手,所以我能理解您的痛苦。
您必须考虑到您正在使用的
JApplet
框架有一个使用默认Layout
的Container
,即BorderLayout
。在这种情况下,您添加的任何内容都将调整为JApplet
的大小(您可能在 HTML 中进行设置)。尝试以下操作:
我使用的一个好的经验法则是,在将所有
Component
对象添加到其父容器之前,将它们粘贴到它们自己的JPanel
中,这样可以避免一些固有的 Swing 布局怪异。另请参阅:Swing 布局指南
Getting the hang of how the Swing classes function can be pretty tricky, so I feel your pain.
You have to consider that the
JApplet
frame that you're using has aContainer
that is using the defaultLayout
, which isBorderLayout
. In this case, any content you add will snap to the size of theJApplet
(which you are likely setting in HTML).Try the following:
A good rule of thumb I use is to stick all
Component
objects into their ownJPanel
before adding them to their parent container, which avoids some inherent Swing layout weirdness.Also, see: Swing Layout Guide