JFrame suddenly contain blue background - java

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.

Related

Java Swing panels and graphics stopped displaying

I'm new to Java Swing (and fairly new to Java in general) and I've been messing around with Swing for a while, but after making some changes my panels stopped displaying and I don't know why.
When using isDisplayable() on my JPanel object it returns false. After more investigation my program does not seem to display graphics at all.
Even a simple piece of code doesn't work for me anymore:
public class window extends JFrame {
window(){
setBounds(100,100,1200,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
}
public static void main(String[] args) throws IOException{
new window();
}
I have no idea what I changed for this to happen, but all I get is a blank a completely blank window.
Any help is appreciated a lot!
See the code comments.
import java.awt.Color;
import javax.swing.*;
public class BlackWindow extends JFrame {
BlackWindow(){
setBounds(100,100,400,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets the frame black, not the content pane
//setBackground(Color.BLACK);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}
public static void main(String[] args) {
new BlackWindow();
}
}
This example also speaks to a few general principles we follow when using Swing's JFrame:
Don't extend the JFrame class, just use an instance of one.
Don't do anything significant to the coloring or styling of a frame, instead color (for example) a JPanel and add it to the frame.

Java drawString() very slow on start up

I'm new to Java programming and am having difficulty with the drawString method delaying almost a full second before drawing after the JFrame appears. Here is the test class I ran to verify it was drawString causing the delay(I'm sorry it is written out, I'm posting on my phone):
import javax.swing.*;
import java.awt.*;
public class Frame1 extends JFrame{
JPanel jp;
Container c;
Frame1(){
setSize(new Dimension(300,300));
c = getContentPane();
c.add(jp = new JPanel(){
public void paint(Graphics g){
g.drawString("hello", 20, 20);
}
});
setVisible(true);
}
public static void main(String args[]){
new Frame1();
}
}
Is there a way to use drawString without this delay? I'm using a MacBook Pro with Mac OSX 10.6.3. Thanks for your help.

Java JPanel Drawing rectangle drawRect(); what to do next, or which component of Java will suit better?

I have drawn a rectangle using a JPanel
My main objective is to store my Requirement Engineering chapter into a JPanel or a JFrame
import java.awt.*;
import javax.swing.*;
class RequirementEngineering extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent();
g.drawRect(10,10,60,60);
g2.drawString("Feasibility study", 20, 20); //rectangle is my main objective, I will look after the string later
}
public static void main(String[] args)
{
}
}
how do I display the JPanel? I know, JApplet doesn't require a main method, but how do we represent JPanel in main() method?
I have this doubt for long, and other posts are confusing me further, Could I have a direct question
My main question being "How to add JFrame to JPanel" pertaining my current coding
thanks in advance
see if you need to use a Window based app, you can do as:
JPanel customPanel = new RequirementEngineering();
JFrame frame = new JFrame("my window");
frame.getContentPane().add(customPanel );
frame.setSize(300,200);
frame.setVisible(true);
If you need in Applet,
public class JAppletExample extends JApplet {
public void init() {
Container content = getContentPane();
JPanel customPanel = new RequirementEngineering();
content.add(customPanel );
}
And you can run it using appletViewer or in any Web Browser such as IE.

Make a Java application invisible to a user

I'm trying to figure out a way to make a Java application invisible to the user.
Basically just trying to remove this
<- Image
How can this be done?
public class TransparentWindow extends JFrame {
public TransparentWindow() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
System.setProperty("sun.java2d.noddraw", "true");
WindowUtils.setWindowTransparent(this, true);
WindowUtils.setWindowAlpha(this, 0.6f);
}
public static void main(String[] args) {
new TransparentWindow().setVisible(true);
}
}
I just seems to have found the answer, just put the line setVisible(false); into comments and you will see the actual program, UNCOMMENT the line to see no trace is left, as far as I can see, that the Java Program is running somewhere, until you won't add the icon to your system tray, manually. Moreover how to remove your Application from Task Manager that question still remains, though you can remove the said icon, as pointed by you in your question.
import javax.swing.*;
public class TransparentWindow extends JFrame
{
public TransparentWindow()
{
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
setOpacity(0.8f);
setSize(200, 200);
//System.setProperty("sun.java2d.noddraw", "true");
//WindowUtils.setWindowTransparent(this, true);
//WindowUtils.setWindowAlpha(this, 0.6f);
setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(this, "It is working!", "Guess : ", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
TransparentWindow tw = new TransparentWindow();
}
}
Here is a snapshot of my desktop on running this program, see the taskbar
Extend from JWindow insted of JFrame. (I did not test this on Windows 7 as I don't sit in front of a Windows box right now. It worked for XP and works for Unity, which surprised me.)
As far as I know, there is no way to remove the task bar icon.

Making a Java panel fullscreen

How would you make a JComponent (panel, frame, window, etc.) fullscreen, so that it also overlaps everything on the screen including the windows start bar?
I don't want to change the resolution or anything with the graphics device like bitdepth etc, I just want to overlap everything else.
Check out this tutorial describing Java's Full-Screen mode API.
Example code (taken from the tutorial). Note that the code operates on a Window so you would need to embed your JPanel with a Window (e.g. JFrame) in order to do this.
GraphicsDevice myDevice;
Window myWindow;
try {
myDevice.setFullScreenWindow(myWindow);
...
} finally {
myDevice.setFullScreenWindow(null);
}
You can try some of the codes in this page, allowing a container to fill the screen (so it is not a solution for an individual component, but for a set of components within a container like a JFrame)
public class MainWindow extends JFrame
{
public MainWindow()
{
super("Fullscreen");
getContentPane().setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
pack();
setResizable(false);
show();
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, getContentPane());
Point l = getLocation();
l.x -= p.x;
l.y -= p.y;
setLocation(l);
}
});
}
...
}
You need to use the following API: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
Going full screen isn't as simple as making a large panel, you need to look into the underlying OS graphics. But your JPanel code should translate just fine.
I needed to search a lot, to do the same. Here is completely a working version of it by steps, so that i can find it later also, and use it.
Step 1: create a file called fullscreen.java
Step 2: copy this code and paste it as it is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class fullscreen extends Window
{
private Button button;
public fullscreen()
{
super(new Frame());
button = new Button("Close");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
add(button);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
}
public static void main(String[] args)
{
// This will take over your whole screen tested and works in my:
// Fedora 12/13/14
// CentOS 5.0
// if this works for you, in other platforms, please leave a comments which OS it worked.
// happy coding!
new fullscreen().setVisible(true);
}
}
Step 3: compile the code and run
Done.
If I were you I would try to make Java not draw the border of the Jframe, then make it take all the screen.
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class FenNoBorder extends JFrame {
public FenNoBorder () {
setUndecorated(true);
setVisible(true);
GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle maximumWindowBounds=graphicsEnvironment.getMaximumWindowBounds();
setBounds(maximumWindowBounds);
}
}

Categories

Resources