2016-07-27 38 views
0

除按钮之外的所有东西都显示在窗口中。有我失踪的东西吗? 这是第一次使用按钮,我不知道发生了什么问题。这可能是一个格式问题。 有人可以告诉我,如果setLocation()和setSize()有问题吗?按钮没有出现在我的Jframe中

import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.TextField; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class HashString extends JPanel { 


    public static void hashString() { 
    } 

    public void window() { 
    JLabel label1 = new JLabel(
      "Enter Your Strings separated by a comma, below. "); 
    label1.setHorizontalAlignment(JLabel.CENTER); 
    label1.setFont(new Font("Times New Roman", Font.BOLD, 12)); 
    label1.setVerticalAlignment(JLabel.TOP); 

    JTextField field = new JTextField(50); 
    field.setVisible(true); 
    field.setText("Enter Strings Here"); 
    field.setSize(300, 251); 
    field.setHorizontalAlignment(JTextField.CENTER); 
    field.setLocation(135, 60); 

    Button btn = new Button("Enter These Values"); 
    btn.setLocation(240 ,420); 
    btn.setSize(100, 100); 
    btn.setVisible(true); 
    btn.setFont(new Font("Times new roman",Font.BOLD,12)); 

    JFrame frame = new JFrame("Test1"); 
    frame.add(new HashString()); 
    frame.add(btn); 
    frame.setVisible(true); 
    frame.add(field); 
    frame.setLocationRelativeTo(null); 
    frame.add(label1); 
    frame.setSize(600, 450); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

}

+0

这是,shoudlve在最后添加了setVisible。 –

回答

2

我想你应该改变你是如何构成的程序。不要将每个职位都放在JFrame内部的手动位置,而应该简单地使用布局管理器,如BorderLayout,这可以很容易地在这里实现。

此外,您应该始终处理从EventQueue而不是任何其他线程的一切。此外,混合和匹配AWT组件如Button和Swing组件如JButton是不可取的。虽然它比以前好得多 - 比如在Java 1.6中 - 但它仍然会出现一些问题。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.border.EmptyBorder; 

public class HashString { // It seems that this isn't a JPanel. Rather, it is an application. 

    JFrame frame; 

    public void initialise() { 

     frame = new JFrame("Test 1"); // You can create the frame of the application here and set title 

     frame.setLocation(200, 200); 
     frame.setSize(300, 300); 

     JPanel contentPanel = new JPanel();  // To allow a border to be set, I've declared a content panel inside the 
               // frame. 
     contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // This sets a border to make everything look nice 
     contentPanel.setLayout(new BorderLayout(5, 5)); // This creates the BorderLayout, which manages the layout of 
                 // the components easily 
     frame.setContentPane(contentPanel); 

     JLabel instructionsLabel = new JLabel("Enter Your Strings separated by a comma, below. "); 
     instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 12)); 
     contentPanel.add(instructionsLabel, BorderLayout.NORTH); // BorderLayout.NORTH tells the layout manager where 
                    // to put the component. 

     JTextField txtField = new JTextField(); 
     txtField.setText("Enter Strings Here"); 
     contentPanel.add(txtField, BorderLayout.CENTER); 

     JButton btn = new JButton("Enter These Values"); 
     btn.setFont(new Font("Times new roman", Font.BOLD, 12)); 
     btn.addActionListener(new ActionListener() { 
      @Override public void actionPerformed(ActionEvent e) { 
       // Call whatever method that you want to call when this is relevant. Set textField and other variables 
       // here. You can do things like 'txtField.setText(methodOperationOnString (txtField.getText()))' or 
       // something of the like. 
      } 
     }); 
     contentPanel.add(btn, BorderLayout.SOUTH); 

     frame.setVisible(true); 

    } 

    public static void main(String[] args) { 
     // This tells it to create the entire thing on the GUI thread 
     EventQueue.invokeLater(new Runnable() { 
      @Override public void run() { 
       HashString b = new HashString(); 
       b.initialise(); 
      } 
     }); 
    } 
} 
+1

哇!这真的很有帮助!我习惯于手动完成所有工作 –

+0

如果你的主要方法是在另一个类中 - 把'public void initialise()'改为'public HashString()'。然后当你调用'new HashString()'时它会自动构造。如果是的话,你也可以删除'main'。 – ifly6

+0

不要混合使用JavaFx和Swing。这是一个Swing应用程序。不要试图从这里初始化它。如果你这样做了(并且你在Eclipse中),你需要删除运行配置。单击“播放”按钮>“运行配置”>“HashString”旁边的箭头,然后单击该运行配置中的删除。 – ifly6