2011-10-05 57 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

你能舉個例子嗎? –