During a JFrame test program, I realized that I can't put this:
setSize(450, 300);
setResizable(false);
setVisible(true);
On the top of the constructor, otherwise it doesn't work as it should. However it works if I put it in the bottom like so:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
setSize(450, 300);
setResizable(false);
setVisible(true);
}
}
It doesn't work if I put it after calling the superclass, but why? I have attempted to put these:
setSize(450, 300);
setResizable(false);
setVisible(true);
One by one on the top to see which was causing the problem, and it seems to to be a this combination:
setSize(450, 300);
setVisible(true);
That doesn't work at the top. However, in one of my other programs it works perfectly fine:
#SuppressWarnings("serial")
public class Client extends JFrame {
private JTextField messageField;
private JTextArea chatWindow;
public Client(String host) {
super("Messenger");
setSize(300, 400);
setVisible(true);
chatWindow = new JTextArea();
chatWindow.setEditable(false);
messageField = new JTextField();
// .. actions etc.
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
add(messageField, BorderLayout.SOUTH);
}
*edit
Working code:
#SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
setSize(450, 300);
setResizable(false);
setVisible(true);
}
}
Glitched code (Screen appears blank):
#SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
setSize(450, 300);
setResizable(false);
setVisible(true);
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
}
}
Related
I'm trying to add a simple JPanel from a different class with only a JButton and a JTextArea into the GUI.
I'm using IntelliJ IDEA for the swing application and there are no errors. However, the JPanel simply doesn't appear/isn't shown.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
public class GUI {
private JPanel mainPanel;
private JPanel mainInner;
public GUI() {
mainPanel.addContainerListener(new ContainerAdapter() {
#Override
public void componentAdded(ContainerEvent e) {
super.componentAdded(e);
System.out.println("component Added");
}
});
}
public void start() {
JFrame frame = new JFrame("GUI");
frame.setContentPane(new GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
InnerPanel inner = new InnerPanel();
mainInner = inner.getMainInner();
inner.setLayout(new BorderLayout());
mainPanel.setLayout(new BorderLayout());
mainPanel.add(mainInner, BorderLayout.CENTER);
mainPanel.revalidate();
mainPanel.repaint();
frame.pack();
frame.setVisible(true);
}
}
The component listener shows that the component has been added, yet it isn't shown.
You forgot to add the mainPanel to frame:
frame.add(mainPanel);
//leaving out import statements
public class MVCView extends JFrame{
private JButton add = new JButton("Click me");
private JTextArea center = new JTextArea(200,300);
private JTextField bottom = new JTextField(200);
public MVCView() {
setLayout(new BorderLayout());
add(add, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public class MVCTester {
public static void main(String[] args) {
MVCView view = new MVCView();
}
}
I'm just trying to get this to show up on my screen. I create a class with main to create the object which is in a different class. When I click run nothing pops up. I've been following a few tutorials and my code doesn't look much different. Already tried putting everything in a JPanel first which didn't work... I don't know what I'm leaving out or doing wrong.
I just modified code you posted and thing works.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MVCView extends JFrame{
private JButton add = new JButton("Click me");
private JTextArea center = new JTextArea(200,300);
private JTextField bottom = new JTextField(200);
public MVCView() {
setLayout(new BorderLayout());
add(add, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MVCView view = new MVCView();
}
}
So I'm making a program that prints out a square brick and a triangular roof. The panel has a background that shows the sky and some grass. Everything seems to be working fine except the buttons that work, when I click the brick button nothing happens. I've tried remodelling the code but no progress.
Here's the program code:
package someapp;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* #author Drew
*/
class Someapp extends JFrame implements ActionListener{
private Graphics land;
private JButton bricks;
private JButton roof;
JButton b1;
JLabel l1;
JLabel background;
public Someapp()
{
setTitle("Buil Your Dream Home");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
background=new JLabel(new ImageIcon("C:\\Users\\Sharon Umute\\Documents\\NetBeansProjects\\someapp\\src\\someapp\\blue-sky.png"));
add(background);
background.setLayout(new FlowLayout());
bricks=new JButton("Bricks");
roof=new JButton("Roof");
background.add(bricks);
background.add(roof);
bricks.addActionListener(this);
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
Someapp frame=new Someapp();
frame.setSize(1280, 735);
frame.setLocation(40, 0);
frame.createGUI();
frame.setVisible(true);
frame.setTitle("Build Your Dream Home");
}
private void createGUI() throws IOException{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window=getContentPane();
window.setLayout(new FlowLayout());
window.add(new JPanelWithBackground("wIcon"));
BufferedImage wPic = ImageIO.read(this.getClass().getResource("blue-sky.png"));
JLabel wIcon = new JLabel(new ImageIcon(wPic));
}
#Override
public void actionPerformed(ActionEvent e) {
{
Graphics paper=background.getGraphics();
paper.drawRect(500, 700, 100, 100);
paper.setColor(Color.red);
}
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
}
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...
Can anybody tell me what is the problem in following program? I want to fit JScrollPane on JtextArea but when I add it then JTextArea is not visible.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Area extends JFrame
{
private JTextArea ta;
private JTextField tf;
JScrollPane jp;
public Area()
{
super("Text Area");
tf=new JTextField();
tf.setBounds(100,350,300,30);
add(tf);
ta=new JTextArea();
ta.setBounds(100,100,300,200);
jp= new JScrollPane(ta);
add(jp);
setLayout(null);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String...s)
{
new Area();
}
}
I see several problems:
Don't use a null layout; do use a real layout.
The default layout of JFrame is BorderLayout; the default position is CENTER; only one component can occupy a position at a time; the example below uses NORTH & CENTER.
Use the appropriate constructor parameters to size the text components initially.
The scrollbar will appear automatically whenever the scrollpane is smaller than the enclosed component; resize the frame to see the effect.
As shown here, the frame's size is made smaller for effect.
See also Initial Threads.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/** #see https://stackoverflow.com/a/19215436/230513 */
public class Area extends JFrame {
public Area() {
super("Text Area");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField(12);
add(tf, BorderLayout.NORTH);
JTextArea ta = new JTextArea(24, 12);
JScrollPane jp = new JScrollPane(ta);
add(jp, BorderLayout.CENTER);
pack();
// arbitrary size to make vertical scrollbar appear
setSize(240, 240);
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Area();
}
});
}
}
Try this:
public Area()
{
super("Text Area");
tf=new JTextField();
tf.setBounds(100,350,300,30);
add(tf);
ta=new JTextArea();
jp= new JScrollPane(ta);
jp.setBounds(5, 5, 100, 100);
add(jp);
setLayout(null);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
You have to use setBounds on JScrollPane, not on JTextArea
sounds like its added but its not shown because of the policy try this:
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);