1

很抱歉发布此信息,但我不知道为什么我的 Array ofJButtons不会显示在我的ButtonsPanel. 添加它们之后,我尝试在单独的类中使用我的代码来测试我的代码并且它有效!但是为什么我的按钮没有出现在这个类中?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class WordGui extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JLabel lblNewLabel_1;
private JPanel buttonPanel;
private JTextArea txtrShuffleHistory;
private Word word ;
private boolean val;
private JButton btnGo;
private JButton button;
private JButton btnShuffleText;
private JPanel panel_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        com.jtattoo.plaf.noire.NoireLookAndFeel.setTheme("Small-Font","","05:Bageo,Dexter");
        UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
    } catch (Throwable e) {
        e.printStackTrace();
    }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                WordGui frame = new WordGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public WordGui() {
    setTitle("Word App Gui");
    setResizable(false);
    setBounds(new Rectangle(100, 100, 600, 200));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 719, 394);
    contentPane = new JPanel();
    contentPane.setBounds(new Rectangle(100, 100, 600, 200));
    contentPane.setSize(new Dimension(600, 250));
    contentPane.setPreferredSize(new Dimension(600, 250));
    contentPane.setBorder(new LineBorder(new Color(204, 0, 255), 5, true));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    panel.setBounds(10, 11, 683, 45);
    contentPane.add(panel);

    panel_1 = new JPanel();
    panel_1.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    panel_1.setBounds(140, 59, 553, 286);
    contentPane.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblNewLabel = new JLabel("Enter a new word here:");
    lblNewLabel.setBounds(10, 11, 183, 14);
    panel_1.add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(224, 8, 157, 20);
    panel_1.add(textField);
    textField.setColumns(10);

    btnGo = new JButton("Go");
    btnGo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource()== btnGo){
                assignWord(textField.getText());

            }
        }
    });
    btnGo.setBounds(391, 7, 57, 23);
    panel_1.add(btnGo);

    JLabel lblOriginalText = new JLabel("Original Text:");
    lblOriginalText.setBounds(10, 36, 84, 14);
    panel_1.add(lblOriginalText);

    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setToolTipText("This is the original text entered\r\n");
    lblNewLabel_1.setHorizontalTextPosition(SwingConstants.CENTER);
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel_1.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    lblNewLabel_1.setBounds(90, 36, 358, 79);
    panel_1.add(lblNewLabel_1);

    JLabel lblShuffledText = new JLabel("Shuffled Text:");
    lblShuffledText.setBounds(10, 126, 110, 14);
    panel_1.add(lblShuffledText);

    buttonPanel = new JPanel();
    buttonPanel.setBorder(new LineBorder(new Color(204, 0, 255), 4, true));
    buttonPanel.setBounds(10, 147, 533, 56);
    panel_1.add(buttonPanel);
    buttonPanel.setLayout(new GridLayout(1, 0, 0, 0));

    btnShuffleText = new JButton("Shuffle Text");
    btnShuffleText.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource() == btnShuffleText){
                shuffle();
            }

        }
    });
    btnShuffleText.setBounds(90, 214, 196, 39);
    panel_1.add(btnShuffleText);

    button = new JButton("Reset");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource() == button){
                textField.setText("");
                getGoBtn().setEnabled(true);
                getShuffleBtn().setEnabled(false);
                getButtonPanel().removeAll();
            }
        }
    });
    button.setBounds(294, 214, 196, 39);
    panel_1.add(button);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBounds(10, 59, 128, 258);
    contentPane.add(scrollPane);

    txtrShuffleHistory = new JTextArea();
    txtrShuffleHistory.setToolTipText("View Shuffle History");
    txtrShuffleHistory.setText("Shuffle History\r\n=================");
    txtrShuffleHistory.setEditable(false);
    scrollPane.setViewportView(txtrShuffleHistory);

    JButton btnClear = new JButton("Clear");
    btnClear.setBounds(10, 322, 126, 23);
    contentPane.add(btnClear);
    valid();
}

public void valid(){
    JTextField field = new JTextField();
    while(val == false){
    int result = JOptionPane.showConfirmDialog(null,field, "Enter a Text?", JOptionPane.OK_CANCEL_OPTION);
    if(result == JOptionPane.OK_OPTION){
        String s = field.getText();
        assignWord(s);
        }
    else if(result == JOptionPane.CANCEL_OPTION){
        getResetBtn().setEnabled(false);
        getShuffleBtn().setEnabled(false);
        val = true;
    }
}
}
public void assignWord(String s){

    try{word = new Word(s); 
    val = true;
    getTextLabel().setText("<html><body><font size = 30 >"+s+"</font></body></html>");
    getTextArea().append("\nOriginal Word :"+s+"\n=================");
    getGoBtn().setEnabled(false);
    getResetBtn().setEnabled(true);
    getShuffleBtn().setEnabled(true);
    }
    catch(RuntimeException e){JOptionPane.showMessageDialog(getParent(),e.getMessage()); val = false;}

}
public void shuffle(){
    word.shuffle();
    getButtonPanel().removeAll();
    String temp = word.getShuffledText();
    JButton[] buttons = new JButton[word.getText().length()];
    for(int x = 0; x < word.getShuffledText().length(); x++){
        buttons[x] = new JButton(""+temp.charAt(x));
        getBody().add(buttons[x]);
        buttons[x].setVisible(true);
    }
    getTextArea().append("\n"+word.getShuffledText());
}
public JLabel getTextLabel() {
    return lblNewLabel_1;
}
public JPanel getButtonPanel() {
    return buttonPanel;
}
public JTextArea getTextArea() {
    return txtrShuffleHistory;
}
public JTextField getTextField() {
    return textField;
}
public JButton getGoBtn() {
    return btnGo;
}
public JButton getResetBtn() {
    return button;
}
public JButton getShuffleBtn() {
    return btnShuffleText;
}
public JPanel getBody() {
    return panel_1;
}
}

我有添加 JButtons 的 shuffle 方法,很抱歉发布这么长的代码,但是有人可以编译它并弄清楚为什么按钮不会显示吗?我尝试更改面板布局但仍然无法正常工作,

这是支持类

    import java.util.*;

    public class Word {
private String text;
private List<Character> charList;
private String shuffled;

public Word(String str) {
    if (str.length() < 3) {
        throw new RuntimeException("Word must be more than 2 characters...");
    }
    if(invalid(str)) {
        throw new RuntimeException("Word must not be composed of a single character...");
    }
    text = str.toUpperCase();
    charList = getChars();
    shuffle();
}



private boolean invalid(String s) {
    int count = 1;
    for (int i = 1; i < s.length(); i++) {
        if (Character.toLowerCase(s.charAt(0)) == Character.toLowerCase(s.charAt(i)))
            count++;
    }
    return (count == s.length());
}

private ArrayList<Character> getChars() {
    ArrayList<Character> tempList = new ArrayList<Character>();
    for (int i = 0; i < text.length(); i++) {
        tempList.add(text.charAt(i));
    }
    return tempList;
}

public void shuffle() {
    String orig = shuffled;
    String tempShuffled;
    do {
        Collections.shuffle(charList);
        tempShuffled = listToString();
    } while (tempShuffled.equals(orig) || tempShuffled.equals(text));
    shuffled = tempShuffled;
}

private String listToString() {
    String strTemp = "";
    for (Character ch: charList) {
        strTemp += ch;
    }
    return strTemp;
}

public String toString() {
    return text;
}

public String getShuffledText() {
    return shuffled;
}

public String getText(){
    return text;
}
}
4

2 回答 2

3

您正在nullJPanel panel_1. JButton 数组中的每个组件都buttons将具有默认大小,0 x 0因此不会出现。

Swing 被设计为使用布局管理器,因此建议始终使用一个。它消除了设置组件尺寸和位置的需要。使用一个panel_1,例如GridLayout

也调用

panel_1.revalidate();
panel_1.repaint();

添加数组中的所有按钮后。

另外:Java 命名约定表明变量使用camelCase代替panelOne.panel_1

于 2013-08-02T23:24:58.027 回答
2
panel_1.setLayout(null);

在这里,您基本上只剩下绝对定位,因此您需要为每个JButtons 设置大小和位置。使用setBounds

于 2013-08-02T23:24:11.280 回答