I should be displaying a row of 2 buttons but that doesn't seem to be the case.
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Studying extends JFrame{
JButton button = new JButton("Word");
JButton button1 = new JButton("MoreWords");
public void Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
public static void main(String[] args){
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500,200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
The constructor is not an actual constructor, it is being treated as a method, causing the classes default constructor to be used. Constructors do not specify a return type, even void.
Fixed Constructor
public Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
The function Studying() isn't a class, so Studying frame = new Studying(); refers to public class Studying extends JFrame and public void Studying() is never called. Move the creation of the buttons to the static main, and append them to the frame, and the buttons will be visible.
public class Studying extends JFrame {
static JButton button = new JButton("Word");
static JButton button1 = new JButton("MoreWords");
public static void main(String[] args) {
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 2));
frame.add(button);
frame.add(button1);
frame.setVisible(true);
}
}
This should work:
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Studying extends JFrame{
JButton button = new JButton("Word");
JButton button1 = new JButton("MoreWords");
public Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
public static void main(String[] args){
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
Related
I am creating a JFrame holding a JPanel(p1) and a JButton(b1)....p1 contains another JPanel(sub1) holding a JLabel(l1)...after i Click on b1,i want a String to be printed in the JLabel...but it is not printing anything.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class panAccDel implements ActionListener {
JFrame frame = new JFrame();
JButton b1;
JLabel l1;
String intro = "Hello there";
JPanel sub1,p1;
public static void main(String[] args) {
panAccDel panAccDel = new panAccDel();
}
public panAccDel(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
b1 = new JButton("Print Data");
sub1 = new JPanel();
p1.add(sub1);
p1.add(b1);
p1.setLayout(new GridLayout(2,1));
sub1.setBackground(Color.RED);
l1 = new JLabel();
sub1.add(l1);
frame.add(p1);
frame.setVisible(true);
frame.setSize(700,700);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
l1.setText(intro);
}
}
}
This is a trial Code.
Any Suggestions???
The listener needs added to the button. For example:
b1.addActionListener(this);
I am trying to add a simple JButton to a JPanel in my program. The problem is when I run the problem, I do not see any button at all.
This is my code:
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuiStopwatch {
public GuiStopwatch() {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
May I know what did I do wrong and how do I fix this?
You are not adding the panel to the frame at any point.
EDIT
Here is the code you would need if you wanted it in a separate method:
import javax.swing.*;
import java.awt.*;
public class GuiStopwatch {
private static void stopwatch(JFrame frame) {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
frame.add(panel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
stopwatch(frame);
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
You could swap things, create everything you need for your frame on the constructor, what makes the code more organized and also you can use it in other Classes, putting on main method will limit what you can do and makes the code not organized
See here an example:
public GuiStopwatch() {
setTitle("Stopwatch");
setSize(600, 600);
// Create JButton and JPanel
JButton button = new JButton("START");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GuiStopwatch guistopwatch = new GuiStopwatch();
}
I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel sidePanel;
public void setup()
{
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocation(100, 50);
mainPanel = new JPanel();
Dimension d = new Dimension(800, 600);
mainPanel.setMaximumSize(d);
mainPanel.setMinimumSize(d);
mainPanel.setPreferredSize(d);
frame.add(mainPanel);
frame.pack();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
}
public void setupPanels()
{
gamePanel = new JPanel();
Dimension d = new Dimension(600, 600);
gamePanel.setMaximumSize(d);
gamePanel.setMinimumSize(d);
gamePanel.setPreferredSize(d);
gamePanel.setBackground(Color.RED);
mainPanel.add(gamePanel);
sidePanel = new JPanel();
d = new Dimension(600, 600);
sidePanel.setMaximumSize(d);
sidePanel.setMinimumSize(d);
sidePanel.setPreferredSize(d);
sidePanel.setBackground(Color.BLUE);
mainPanel.add(sidePanel);
}
public void setupGame()
{
GameArea game = new GameArea();
gamePanel.add(game);
game.start();
}
public static void main(String[] args)
{
Main main = new Main();
main.setup();
main.setupPanels();
main.setupGame();
}
This question already has answers here:
JFrame not presenting any Components
(4 answers)
Closed 7 years ago.
When i run this code:
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public void main(String args[]){
new Menu();
}
Menu(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setSize(500, 300);
frame.setVisible(true);
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
}
public void actionPerformed(ActionEvent e){
}
}
The window opens but it does not show anything... Where is my label? I am very lost because I have done various GUI before and either I am being stupid or i don't know! Sorry if its a stupid question, I'm just so stuck I had to post this.
Your code works for me, but I think it doesn't for you because you add a component after you set the frame visible. Call frame.setVisible(true) (and setSize) after
frame.add(north, BorderLayout.NORTH);
north.add(logo);
So your code should look like this (also formatted it properly):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public static void main(String args[]) {
new Menu();
}
public Menu() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
frame.setSize(500, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
}
I am trying to create this Panel into a class but it is not working, trying to make it go into the Frame as well. I am getting the "It is not a class error"
Please explain to me what I am doing wrong. Programming is fun until you are stuck for hours on one problem.
Panel:
import java.awt.Button;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TopPanel extends JPanel {
public TopPanel{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Create a frame");
frame.getContentPane().add(panel);
Button button = new Button("111");
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
Frame:
import javax.swing.*;
import java.awt.*;
public class CourseGUI extends JFrame {
public CourseGUI()
{
super("CourseGUI Frame");
JPanel topPanel = new JPanel();
topPanel.setBackground(java.awt.Color.WHITE);
Dimension d = new Dimension(800,600);
topPanel.setPreferredSize(d);
this.setLayout(new BorderLayout());
this.add(topPanel, BorderLayout.NORTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
this.setVisible(true);
}
public static void main(String[] args)
{
new CourseGUI();
}
}
Thanks in advanced.
I changed the TopPanel:
import javax.swing.*;
import java.awt.*;
public class TopPanel extends JPanel {
public TopPanel(){
JPanel panel = new JPanel();
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
}
}
TopPanel is your class name, topPanel is your JPanel instance. (Java is case sensitive).
Lines like
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
Are trying to use the class which is not what you intended...
Your are also missing () on the line public TopPanel { (the one inside the class, not the one defining the class)
Crse is a local variable in the TopPanel creator, so you can't use it inside CourseGUI()
TopPanel is creating a frame to put itself into which is weird...