2011-10-05 81 views
1

我是Java Applet编程和Java的新手,但我在C#和C++中非常出色。无论如何,我正在用JTextField,JButton和JLabel制作一个简单的计算器。但JButton和JTextField占据了整个屏幕,最奇怪的是我设置了大小,并没有设置大小。那么我做错了什么?这里是下面的代码,所以有人可以帮助我;我会感谢任何答案。添加JButton控件到JApplet并且JButton填充整个屏幕

/* 
* 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 
} 

回答

4

您需要在这里考虑布局。 JApplet的contentPane默认使用BorderLayout,如果您将组件添加到BorderLayout而没有第二个参数来告诉它将其添加到何处,它将被添加BorderLayout.CENTER并将尽可能多地占用空间。解决这个问题的关键是尽可能多地了解布局经理,并将这些信息用于您的优势。你可以在这里找到这个信息:Visual Guide to the Layout Managers

请注意,我们经常嵌套JPanels,每个JPanel使用自己的编码器友好的布局管理器,以实现复杂但易于管理的布局。

例如,

import java.awt.*; 
import javax.swing.*; 

public class Applet extends JApplet { 

    private JButton button1; 
    private JTextField textBox1; 

    @Override 
    public void init() { 
     button1 = new JButton("Calculate"); 
     textBox1 = new JTextField("number"); 
     textBox1.setFont(new Font("Courier", Font.PLAIN, 12)); 
     JPanel myPanel = new JPanel(); // JPanel uses FlowLayout by default 
     myPanel.add(new JLabel("Enter a number: ")); 
     myPanel.add(textBox1); 
     myPanel.add(button1); 
     myPanel.setBackground(Color.gray); 

     getContentPane().add(myPanel, BorderLayout.CENTER); 
    } 

} 

还要注意的setSize通常是由大多数布局管理器忽略。相反,preferredSize通常是可以接受的,但是应该避免设置它,相反,您希望组件本身根据其内容或其他属性(例如JTextArea的行和列或JTextField的列或String文本对于JLabel)。

2

抓住Swing类的功能可能非常棘手,所以我感到你的痛苦。

你必须要考虑的是JApplet框架您使用具有使用默认Layout,这是一个BorderLayoutContainer。在这种情况下,您添加的任何内容都会捕捉到JApplet(您可能在HTML中设置)的大小。

尝试以下操作:

public void init() { 
     setLayout(new FlowLayout()); 
     button1 = new JButton("Calculate"); 
     textBox1 = new JTextField("number"); 
     textBox1.setFont(new Font("Courier", Font.PLAIN, 12)); 
     textBox1.setSize(100, 36); 
     button1.setSize(100, 36); 

     JPanel contentPanel = new JPanel(); 
     contentPanel.add(button1); 
     contentPanel.add(textBox1); 

     getContentPane().add(contentPanel); 
     c.setBackground(Color.gray); 
} 

一个好的经验法则我用的是所有Component对象坚持到自己JPanel他们加入到他们的父容器,避免了一些固有的Swing布局的怪事了。

此外,见:Swing Layout Guide

+0

很抱歉,但我跟你的 “经验法则” 不同意。通常将组件放在自己的JPanel中并不是一个好主意。更好的方法是了解布局管理器并使用这些知识正确使用它们。 –

+0

即使使用正确的布局过程如果您尝试将“原始”组件添加到容器,Swing会出现奇怪的情况。我不是说每个组件都粘贴到单独的JPanel中。 –

+0

你能举个例子吗? –