Here you will see my code:
I am just trying to make a little window that displays "Hello, Java!".
I am currently running on Ubuntu 14.04. To go more in depth with my problem, the icon with the coffee cup shows up when I run the program like there is a window, but there is not window attached to it and if clicked, no window pops up.
Any help would be much appreciated!
public class HelloJava1 extends javax.swing.JComponent
{
public static void main(String[] args)
{
javax.swing.JFrame f = new javax.swing.JFrame("HelloJava1");
f.setSize(300, 300);
f.getContentPane().add(new HelloJava1());
f.setVisible(true);
}
public void paintComponent(java.awt.Graphics g)
{
g.drawString("Hello, Java!", 125, 95);
}
}
Additionally, I am compiling via command line using javac HelloJava1.java and running using java HelloJava1.
I am writing the code via gedit.
This code should work reliably:
import java.awt.*;
import javax.swing.*;
public class HelloJava1 extends JComponent {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("HelloJava1");
// f.setSize(300, 300); better to pack() the frame
f.getContentPane().add(new HelloJava1());
// pack should be AFTER components are added..
f.pack();
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
SwingUtilities.invokeLater(r);
}
#Override // good practice..
public void paintComponent(java.awt.Graphics g) {
// always call super method 1st!
super.paintComponent(g);
g.drawString("Hello, Java!", 125, 95);
}
// instead of setting the size of components, it is
// better to override the preferred size.
#Override
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
}
Related
I am trying to create a simple program using JInternalFrame on Swing and when i run my code, it suddenly produces a blue background. Can anyone tell me how i can remove it?
here is the code i tried
import javax.swing.*;
public class Main extends JFrame {
JDesktopPane dp = new JDesktopPane();
JInternalFrame intf = new JInternalFrame("demo");
public void initialize() {
setTitle("Test Program");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public Main() {
intf.setSize(150, 200);
intf.setVisible(true);
dp.add(intf);
add(dp);
initialize();
}
public static void main(String args[]) {
new Main();
}
}
It's part of the PL&F.
To literally remove it you can make the JDesktopPane non-opaque:
dp.setOpaque(false);
Or set the background to a colour that you like:
dp.setBackground(new java.awt.Color(200,200,200));
But it looks weird with a light colour.
There's probably someway of configuring the macOS PL&F. All just live with macOS looking like macOS wants to look.
I hope this isn’t a stupid first question; I can’t seem to find an answer anyway.
I have this JFrame constructor where a JPanel is added to the JFrame. The JPanel paints a Rectangle in the JFrame, and that’s fine. However, if I add an ImageIcon object as in the code below (for later use), the rectangle isn’t painted. It does appear if I resize the window though.
One solution is to put the setVisible(true) as the last line, or to instantiate the ImageIcon above the constructor, but I really want to understand this. It doesn’t make sense to me that an object not even used can cause this behaviour. Thanks.
public class AJFrame extends JFrame {
ImageIcon ii;
public AJFrame() {
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
ImageIcon ii = new ImageIcon("Untitled.png");
JPanel jp = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
g.fillRect(0, 0, 50, 50);
}
};
add(jp);
}
public static void main(String[] args) {
AJFrame jf = new AJFrame();
}
}
All actions within a frame should be done in the EDT (Event Dispatching Thread) of Swing. Therefore the right way to start your frame is
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AJFrame().setVisible(true);
}
});
So maybe it all comes down to the wrong start of your frame.
The main routine of a Java program is not started within the EDT. All Swing actions that are not within the EDT could produce strange refresh/visibility issues.
Here is the complete sourcecode:
public class AJFrame extends JFrame {
ImageIcon ii;
public AJFrame() {
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//setVisible(true);
//ImageIcon ii = new ImageIcon("Untitled.png");
JPanel jp = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
g.fillRect(0, 0, 50, 50);
}
};
add(jp);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AJFrame().setVisible(true);
}
});
}
}
I set a JPanel as a contentPane of my JFrame.
When I use:
jPanel.setBackground(Color.WHITE);
The white color is not applied.
But when I use:
jFrame.setBackground(Color.WHITE);
It works... I am surprised by this behaviour. It should be the opposite, shouldn't it?
SSCCE:
Here is an SSCCE:
Main Class:
public class Main {
public static void main(String[] args) {
Window win = new Window();
}
}
Window Class:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
private Container mainContainer = new Container();
public Window(){
super();
this.setTitle("My Paint");
this.setSize(720, 576);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainContainer.setBackground(Color.WHITE); //Doesn't work whereas this.setBackground(Color.WHITE) works
this.setContentPane(mainContainer);
this.setVisible(true);
}
}
Container Class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Container extends JPanel {
public Container() {
super();
}
public void paintComponent(Graphics g) {
}
}
The reason is very simple include the following line
super.paintComponent(g);
when you override paintComponent.
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
Now it works perfectly.
You should always do this unless you have a very specific reason to do so .
[PS:Change the colour to red or something darker to notice the difference as sometimes it becomes difficult to differentiate between JFrame's default grey colour and White colour]
With my testcode it works the way you expected it to work:
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(new Dimension(400,400));
f.setLocationRelativeTo(null);
JPanel p = new JPanel();
p.setSize(new Dimension(20,20));
p.setLocation(20, 20);
//comment these lines out as you wish. none, both, one or the other
p.setBackground(Color.WHITE);
f.setBackground(Color.BLUE);
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
I know this question has been answered before, but it's just not working for me. I followed the instructions from here: How to change JProgressBar color?
import javax.swing.*;
import java.awt.*;
public class ProgressBarTest extends JFrame {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UIManager.put("ProgressBar.background", Color.orange);
UIManager.put("ProgressBar.foreground", Color.black);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
JProgressBar progressBar = new JProgressBar(0,100);
progressBar.setValue(50);
f.add(progressBar, BorderLayout.PAGE_END);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
All I am getting is the same old colors.
I'm using Mac OS X 10.7.3 and Java 1.6. I tried the CrossPlatformLookAndFeel and it works with the new colors. However I want this in the default look and feel. How can I do this?
To override Look & Feel defaults, make the change before constructing the GUI on the event dispatch thread, as shown below.
On the com.apple.laf.AquaLookAndFeel, the progress bar's UI delegate is an instance of com.apple.laf.AquaProgressBarUI. As you have found, it ignores many defaults in favor of the native component. If a novel color scheme is required, consider supplying your own UI delegate, as shown here.
AquaProgressBarUI:
CustomProgressUI:
ProgressBar UI Defaults:
ProgressBar.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
ProgressBar.border: javax.swing.plaf.BorderUIResource#47f08ed8
ProgressBar.cellLength: 1
ProgressBar.cellSpacing: 0
ProgressBar.cycleTime: 3000
ProgressBar.font: sun.swing.SwingLazyValue#6446d228
ProgressBar.foreground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.horizontalSize: javax.swing.plaf.DimensionUIResource[width=146,height=12]
ProgressBar.repaintInterval: 20
ProgressBar.selectionBackground: javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
ProgressBar.selectionForeground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.verticalSize: javax.swing.plaf.DimensionUIResource[width=12,height=146]
ProgressBarUI: com.apple.laf.AquaProgressBarUI
SSCCE:
import java.awt.*;
import javax.swing.*;
public class ProgressBarTest extends JFrame {
public static void main(String args[]) {
UIManager.put("ProgressBar.repaintInterval", 100);
UIManager.put("ProgressBar.border",
BorderFactory.createLineBorder(Color.blue, 2));
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
f.setLayout(new GridLayout(0, 1, 5 , 5));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createBar());
f.add(createBar());
f.add(createBar());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JProgressBar createBar() {
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(50);
return progressBar;
}
});
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyDrawPanel extends JPanel{
public void paintComponents(Graphics g){
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().add(paintComponents(g));
frame.setVisible(true);
}
}
I think I should add something arguments in frame.getContentPane().add(paintComponents(g));.
I looked up Graphics class but I'm still struggling with it. What should be the parameter of it?
try this
public class MyDrawPanel extends JPanel{
MyDrawPanel()
{
setOpaque(true);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyDrawPanel());
frame.setBounds(10,10,500, 500);
frame.setVisible(true);
}
}
I'm no awt expert, but what I think you want to do is add a Canvas object to your content pane from the JFrame, then paint a Graphics object on it.
Okay, this is what I came up with:
public class MyDrawPanel extends JPanel
{
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
MyDrawPanel panel = new MyDrawPanel();
panel.setOpaque(true);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
createAndShowGUI();
}
});
}
}
Notes:
There are several problems with your line frame.getContentPane().add(paintComponents(g));. What you said is "add to the content pane the result of calling paintComponents on g. Where did g come from? You can not used a variable until declared. The result of calling paintComponents is void which means the result cannot be used as an argument to a method. Presumably you had compiler errors.
I changed paintComponents to paintComponent. The former is used to control painting of subcomponents and in general should not be overridden.
Swing objects should not be created on the main thread. The details are complicated for a beginner (and described here in detail). Mostly you can just memorize the SwingUtilities.invokeLater pattern used above.