I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being properly displayed. My code is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.setContentPane(panel);
}
}
My desired layout is something very similar to this:
Could someone advise as to why this is.
Thanks for any help :)
Related
I have tried creating a button "Menu", and on click it closed the frame that it's on, open a class StartWindow, after that it would use a method "Menu", without running method "Welcome". But in stead it doesn't close the frame, but also runs just the class, which at first runs only method "Welcome"
Button:
menu.setBounds(1, 1, 65, 65);
menu.setFocusable(false);
menu.setText("Menu");
menu.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent j) {
if(j.getSource() == menu) {
StartWindow menu = new StartWindow();
frame.dispose();
menu.Menu(frame);
}
}
});
Constructor
StartWindow(){
JFrame frame = new JFrame();
frame.setSize(420, 420);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Welcome(frame);
}
Method:
```Java
public void Menu(JFrame frame){
JPanel panel1 = new JPanel();
JTextField text1= new JTextField();
JTextField text2= new JTextField();
JButton button1= new JButton();
JButton button2= new JButton();
panel1.setLayout(null);
panel1.setSize(420, 420);
panel1.setBackground(new Color(212, 190, 190));
text1.setText("With friends");
text1.setBackground(new Color(212, 190, 190));
text1.setBounds(140, 60, 65, 40);
text1.setBorder(null);
text1.setFocusable(false);
text1.setEditable(false);
text2.setText("With AI");
text2.setBackground(new Color(212, 190, 190));
text2.setBounds(140, 132, 65, 40);
text2.setBorder(null);
text2.setEditable(false);
text2.setFocusable(false);
//With friend
button1.setBounds(140, 100, 85, 32);
button1.setBackground(new Color(212, 190, 190));
button1.setFocusable(false);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent a) {
if(a.getSource() == button1) {
frame.dispose();
new TTT_1();
}
}
});
button1.setBorder(new RoundedBorder(10));
button1.setText("1 VS 1");
//AI
button2.setBounds(140, 172, 75, 32);
button2.setBackground(new Color(212, 190, 190));
button2.setFocusable(false);
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent b) {
if(b.getSource() == button2) {
frame.dispose();
new TTT_2();
}
}
});
button2.setBorder(new RoundedBorder(10));
button2.setText("AI");
panel1.add(text1);
panel1.add(text2);
panel1.add(button1);
panel1.add(button2);
frame.add(panel1);
frame.setVisible(true);
}
I had a few theories on what is wrong, one of them is the constructor that it can be redesigned and it will solve my problem, another was that there is something wrong in the button.
I have an issue with my code below, the buttons and the Panel aren't displayed until mouseover
Could you please help me to identify the reason of this issue ?
Thank you in advance for your help.
Kind regards
JButton atelier;JButton examen;
private JPanel contentPane;
public void principe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50,1000,700);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel Evenement = new JPanel();
Evenement.setBounds(3, 200, 251, 62);
contentPane.add(Evenement);
Evenement.setLayout(null);
Evenement.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
choix();}
});
JLabel lblNewLabel_5 = new JLabel("Activité");
lblNewLabel_5.setBounds(128, 13, 111, 36);
Evenement.add(lblNewLabel_5);
}
public void choix() {
JPanel m= new JPanel();
m.setBackground(new Color(70, 130, 180));
m.setBounds(260,20, 689, 75);
m.setLayout(null);
atelier = new JButton("Atelier");
atelier.setBounds(486,15, 137, 40);
atelier.setBackground(new Color(200, 180, 150));
m.add(atelier);
examen = new JButton("Examen");
examen.setBounds(344,15, 137, 40);
examen.setBackground(new Color(200, 180, 150));
m.add(examen);
contentPane.add(m);
}
I m creating a GUI in java and would like to use a JTextArea, however I am having a lot of trouble adding it to the frame. How would I go about creating a text Area and then using it to read text or display text?
Here is my GUI code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class addMemoUI extends JFrame {
JFrame frame = new JFrame();
/**
* Create the application.
*/
public addMemoUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
JButton button = new JButton("Create");
button.setBackground(new Color(100, 149, 237));
button.setBounds(135, 350, 130, 50);
frame.getContentPane().add(button);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(22, 21, 234, 37);
frame.getContentPane().add(lblMemos);
JButton button_1 = new JButton("Cancel");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(5, 350, 130, 50);
frame.getContentPane().add(button_1);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MemoUI window = new MemoUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Thanks very much :)
Here is example for how to use JTextArea. You can set, get or append text. You can find the others by google.
public class Example {
private JTextArea jtextbox;
private void initialize() {
JFrame frm = new JFrame();
:
JScrollPane scroll = new JScrollPane();
jtextbox= new JTextArea();
scroll.setViewportView(jtextbox); // add scroll panel
jtextbox.setTabSize(4);
jtextbox.setLineWrap(true);
jtextbox.setBackground(SystemColor.window);
}
private void setText(String text) {
jtextbox.append(text); // or setText(text)
}
private String getText() {
return jtextbox.getText();
}
}
I was looking in to my Java project when I realized I have yet to make my title screen. But one problem came to mind: How do I make it in where when they press on the new file button it will clear everything on the screen and put the new stuff in?
In simpler words, how to make a action listener so when they click the button the screen will clear and put a new screen on?
package Main_Config;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class SET_UP extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JLabel consol;
public static Dimension size = new Dimension(800, 700);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SET_UP frame = new SET_UP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SET_UP() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(370, 70, 0, 0);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setSize(size);
setLocationRelativeTo(null);
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
consol.setText(input);
}
});
textField.setBounds(10, 452, 243, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
enter.setBounds(253, 452, 89, 20);
contentPane.add(enter);
JLabel consol = new JLabel("");
consol.setBounds(0, 483, 335, 189);
contentPane.add(consol);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(352, 451, 200, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(584, 451, 200, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(0, 0, 89, 23);
contentPane.add(btnNewButton_2);
}
}
To clear the screen, I do this:
Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
frame.remove(panelTwo);
frame.remove(panelThree);
frame.add(panelFour);
frame.setVisible(true);
}
});
I'm currently working on a game for school, and I've hit a brick wall. I've created a main class that sets up the JFrame, and in that JFrame have JPanel buttons that open a server JPanel class, a client JPanel class, and buttons for options, and exiting the game. Now where I'm stuck is how I should make buttons to go back to the main JPanel using a back button on the server/client JPanel class. Here's the code I have at the moment:
MainUI.class (a different class runs this):
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ServerUI.class:
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ClientUI.class:
public class ClientUI extends JPanel {
private JTextField textField;
public ClientUI() {
setLayout(null);
JButton btnConnect = new JButton("Connect");
btnConnect.setBounds(47, 400, 200, 25);
add(btnConnect);
JButton btnBack = new JButton("Back");
btnBack.setBounds(117, 436, 60, 25);
add(btnBack);
JRadioButton rdbtnSelectAServer = new JRadioButton("Select a server from the list:");
rdbtnSelectAServer.setBounds(66, 25, 161, 25);
add(rdbtnSelectAServer);
JRadioButton rdbtnManualConnection = new JRadioButton("Manual Connection:");
rdbtnManualConnection.setBounds(87, 325, 120, 25);
add(rdbtnManualConnection);
textField = new JTextField();
textField.setBounds(47, 355, 200, 25);
add(textField);
textField.setColumns(10);
}
}
I've heard that I could use a CardLayout, too, but I want to see if I can do it this way.