i have built an Swing based GUI, which builds successfully. But after running, it only displays a blank, white frame with no text, button whatsoever on it.
I've already checked that the GUI elements are added to the panel (myPanel.add(bTest, "Card1");
). Also the $$$setupUI$$$();
should execute after the constructor.
Code:
package voc;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GUI extends JFrame {
private JPanel myPanel;
private JButton bTest;
private JTextArea tEnterTranslation;
public GUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
bTest.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
}
});
}
public static void create() {
GUI gui = new GUI();
}
public static void main(String[] args) {
create();
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
myPanel = new JPanel();
myPanel.setLayout(new CardLayout(0, 0));
myPanel.setMaximumSize(new Dimension(500, 500));
myPanel.setMinimumSize(new Dimension(500, 500));
myPanel.setPreferredSize(new Dimension(500, 500));
myPanel.setBorder(BorderFactory.createTitledBorder(null, "voc", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font(myPanel.getFont().getName(), myPanel.getFont().getStyle(), myPanel.getFont().getSize()), new Color(-4497096)));
bTest = new JButton();
bTest.setMinimumSize(new Dimension(10, 20));
bTest.setText("Button");
myPanel.add(bTest, "Card1");
tEnterTranslation = new JTextArea();
tEnterTranslation.setMinimumSize(new Dimension(20, 15));
tEnterTranslation.setPreferredSize(new Dimension(20, 15));
tEnterTranslation.setText("");
myPanel.add(tEnterTranslation, "Card2");
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return myPanel;
}
}
You forgot to add the JPanel
to your JFrame
's contentPane()
public GUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(myPanel); <--------------------------------- HERE
this.setVisible(true);
bTest.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
}
});
}
If I were you, I'd also use JFrame#setSize(Dimension)
because your JFrame
will now appear with the minimum size.