Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to write a small GUI program that consist of one JComboBox that has to paint the ContentPane with a color chosen from the combobox.
I do not know why it doesn't work as it is supposed to. When I start my main method it produces an empty JFrame even without the comboBox, which I know that I had added to the JFrame.
This is my main method:
import javax.swing.JFrame;
public class TestRGBComboBox {
public static void main(String[] args) {
JFrame frame = new RgbComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
this is my JFrame class
public class RgbComboBoxFrame extends JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;
private JComboBox colorComboBox;
private ActionListener listener;
public void RgbComboBoxFrame() {
colorComboBox = new JComboBox();
colorComboBox.addItem("RED");
colorComboBox.addItem("GREEN");
colorComboBox.addItem("BLUE");
colorComboBox.setEditable(true);
listener = new AddListener();
paintContentPane();
createPanel();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent event){
paintContentPane();
}
}
private void paintContentPane(){
Color c = (Color)colorComboBox.getSelectedItem();
getContentPane().setBackground(c);
}
private void createPanel(){
JPanel controlPanel = new JPanel();
colorComboBox.addActionListener(listener);
controlPanel.add(colorComboBox);
add(controlPanel,BorderLayout.SOUTH);
}
}
Although all the code is there to create the panel and add things to it you never call them. public void RgbComboBoxFrame(){} should be a constructor and therefore written as public RgbComboBoxFrame(){}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am trying to add a GIF to the frame. Here is my code but when I run it cannot see GIF. Is there a function to add GIF to java?
Any help would be appreciated.
public class confetti extends JFrame
{
private static JFrame frame;
public confetti() {
frame = new JFrame();
frame.setSize(510, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
addConfetti();
}
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("confetti-40.gif"));
frame.add(jpCenter,BorderLayout.CENTER);
lblgif.setVisible(true);
}
public static void main(String args[]) {
new confetti();
}
}
You never add your label to any frame/panel. In your code it might be something like this:
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("path/to/your/confetti-40.gif"));
frame.add(jpCenter, BorderLayout.CENTER);
jpCenter.add(lblgif);
lblgif.setVisible(true);
}
Also check your absolute path from your projecto to your gif file, your code in works for me on my PC.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Whilst following through the Java in Easy Steps book I came across a error whilst creating my first GUI.
import javax.swing.*;
public class Main extends JFrame{
JPanel pnl = new JPanel();
public Window() {
super("Menu Window");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(pnl);
setVisible(true) ;
}
public static void main(String[] args) {
Main gui = new Window();
}
}
And the error I get is:
'Error: Could not find or load main class main'
I am using Eclipse and this is the only thing that shows in the console.
The constructor name should be the same as the class name (thus Main not Window), So we have:
import javax.swing.*;
public class Main extends JFrame{
JPanel pnl = new JPanel();
public Main() {//Watch this line carefully (Window changed to Main)
super("Menu Window");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(pnl);
setVisible(true) ;
}
public static void main(String[] args) {
Main gui = new Main();//Watch here too
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have stucked to 4 lines of code.
They supposed to let me create my own widget.
But it seems that the main method is missing.
I have tried to write a main class on my-own and create an object in paintComponent() method with no luck.
I know that i am not supposed to call the method myself, but when i run the program i get the error Main method not found in class....
Can someone please explain me how this works or give me a link to read?
Here is the simple code that I tried:
import javax.swing.*;
import java.awt.*;
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
}
Try this simple code, it should be helpful to you:
import javax.swing.*;
import java.awt.*;
public class MyDrawPanel extends JPanel {
private static final int WIDE = 300;
private static final int HIGH = 200;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
public MyDrawPanel() {
setBackground(Color.white);
// Set a initial size for the program window
this.setPreferredSize(new Dimension(WIDE, HIGH));
}
private void display() {
// Some statements to let the JFrame appear in a good way
JFrame f = new JFrame("MyDrawPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
// Main method is called when the program is runned
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MyDrawPanel().display();
}
});
}
}
Output:
If you are new to java, I would suggest to just give 4-5 days on Tutorials | Javatpoint.
Java rule is that the java file name should match the class name containing main method.
So a simple code of main method:
class Simple {
public static void main(String args[]) {
System.out.println("Hello Java");
}
}
In this case this code should be in Simple.java file.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm getting the following error : cannot find symbol color b=new color(Color.BLACK);
EDIT: I'm trying to create an instance of the class color.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class eventqueue{
public static void main(String args[]){
final JFrame frame=new JFrame("PROGRAM");
final JPanel panel=new JPanel();
EventQueue.invokeLater(new Runnable(){
public void run(){
final JButton black=new JButton("BLACK");
panel.add(black);
frame.add(panel);
color b=new color(Color.BLACK);
black.addActionListener(b);
frame.setVisible(true);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class color implements ActionListener{
public Color c;
public color(Color bc){
c=bc;
}
public void actionPerformed(ActionEvent event){
panel.setBackground(c);
}
}
}
});
}
}
Since color is class that is locally declared in a run() method. Now that class must be declared before it's use.
Sample code:
EventQueue.invokeLater(new Runnable() {
public void run() {
...
// MOVE it HERE
class color implements ActionListener {
public Color c;
public color(Color bc) {
c = bc;
}
public void actionPerformed(ActionEvent event) {
panel.setBackground(c);
}
}
// Now use it HERE
color b = new color(Color.BLACK);
black.addActionListener(b);
...
}
});
Note: follow Java Naming convention.
Declare the variable as,
Color b = new Color(Color.BLACK);
Change your code
Color b = new Color(Color.BLACK);
to represent the Color class. Which is the thing you want in your code.
You can even see that there is a color (I really mean to say color) difference of both Color and color. Lowercase is the variable name and Uppercase is the Class name.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Basically, I'm trying to do a test on my GUI to make sure it will paint.
Here are my classes:
Game
public class Game {
private static GUI gui = new GUI();
private static int[][] pixels = new int[10][10];
public static void main(String[] args) {
}
public void startGame() {
System.out.print("start");
gui.setGameFrame();
}
public static GUI getGUI() {
return gui;
}
public static int[][] getGraphics() {
return pixels;
}
}
GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JPanel {
private static Game game = new Game();
private static JPanel panel = new JPanel();
private static JFrame frame = new JFrame();
final private static int FRAME_HEIGHT = 500;
final private static int FRAME_WIDTH = 500;
//Board size 25x25px
final private static int PIXEL_SIZE = 20;
public GUI () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setAttributes();
makeMenu();
}
});
}
public static void setAttributes() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("");
frame.setBackground(Color.black);
frame.setVisible(true);
}
private static void makeMenu() {
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.startGame();
}
});
panel.add(start);
frame.add(panel);
frame.pack();
}
public void setGameFrame() {
panel.removeAll();
frame.getContentPane().add(Game.getGUI());
frame.setTitle("Snake v0.1");
frame.setSize(getPreferredSize());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(5, 5, 10, 10);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
}
public void paintGraphics() {
int[][] pixels = Game.getGraphics();
}
}
I've attempted to debug it, but cannot trace why it isn't functioning.
I believe it's something to do with:
frame.getContentPane().add(Game.getGUI());
But I'm not certain.
Me: "What's the purpose of the Game class? Why not just run everything from the Gui class? "
You: "because further code will involve logic and event handling, and sending these events to the GUI class for updating."
Think of the Game as your data model. Use the Game class for only data and data manipulation and keep all the GUI procedures in your GUI class. Just create an instance of the Game class in your GUI class. Run the program from your GUI class, i.e. have the main method in your GUI class. and run the invokeLater from the main method.
I copied and pasted your code directly into Netbeans, and it seems to be working.
On mine there seems to be some graphical object covering the top left of the white rectangle, which is why it didn't show up initially.
Try changing your fillRect() function to fill a larger area.
This worked for me:
g.fillRect(5, 5, 100, 100);