I have worked a bit with creating basic GUI's in java. Very simply, what I am trying to achieve, is a title in a given font, and below that a JPanel containing 5 different buttons equally spaced and sized, with a modifier function that allows for the resolution to be changed by a scale factor of say 0.75, the JPanel would not touch the edge of the screen, but have a border of 20 pixels away. What I would love to achieve is a way of the user being able to enter whatever resolution they want and for it to still keep the same basic design therefore making it compatible with other devices.
.
The code I have been using thus far is:
package prototype1;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
public static void main(String[] args){
stateManager();
}
static public JFrame menu = new JFrame("Menu Screen");
static public double modifier = 1;
static public int width = 750;
static public int height = 1334;
static void stateManager(){
JFrame.setDefaultLookAndFeelDecorated(true);
AL demo = new AL();
menu.setContentPane(demo.contentPanel1());
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setSize((int) (width * modifier),(int) (height * modifier));
System.out.println((int)(width * modifier));
menu.setVisible(true);
}
static class AL implements ActionListener{
JLabel titleLabel;
JButton checkStar;
JPanel buttonScreenMenu;
public JPanel contentPanel1(){
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
totalGUI.setBackground(Color.white);
int x = width;
int y = height;
titleLabel = new JLabel("Select what it is you would like to do!");
titleLabel.setFont(new Font("Castellar",Font.PLAIN, (int) (18 * modifier)));
titleLabel.setLocation(0,(int) (40 * modifier));
titleLabel.setSize((int) (x * modifier),(int) (30 * modifier));
titleLabel.setHorizontalAlignment(0);
totalGUI.add(titleLabel);
buttonScreenMenu = new JPanel();
buttonScreenMenu.setLocation((int) (20 * modifier) , (int) (100 * modifier));
buttonScreenMenu.setSize((int) ((x - 40) * modifier),(int) ((y - 120) * modifier));
buttonScreenMenu.setBorder(BorderFactory.createLineBorder(Color.black));
totalGUI.add(buttonScreenMenu);
checkStar = new JButton("Work out a Star");
checkStar.setLocation((int)(20 * modifier),(int)(20 * modifier));
checkStar.setSize(buttonScreenMenu.getWidth() - 40, (int) (buttonScreenMenu.getHeight() / 4) - 40);
checkStar.setBackground(Color.white);
buttonScreenMenu.add(checkStar);
return totalGUI;
}
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}
However I stopped here as I was not getting anywhere near the results I wanted. The output of this code looks as follows:
How to fix the resolution problem?
Off of what cricket said, using absolute position will cause and give you many problems. I suggest you to look into using a layout manager such as box layout, which looks exactly same way you want to display your nodes. All you need to do is add spacing between the nodes. Which you can see how to do here.
Thus alternatively if you still want the users to enter in the dimensions of their frames. All you have to do is set the dimensions of your jframe and every thing will automatically adjust itself.
Related
Wondering if anyone knows how to disable line break when using html in JLabel.
Here is the code:
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
public class Main
{
private static final int[] TEXT =
{ 0x05D0, 0x05B2, 0x05DC,
0x05B5, 0x05D9, 0x05DB,
0x05B6, 0x05DD
};
public static void main(String[] args)
{
String text = "";
for(int cp : TEXT)
text += Character.toString(cp);
String html = "<html>" + text + "</html>";
JLabel label = new JLabel(html);
JLabel msg = new JLabel("The text should at least go out to here.");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(label);
panel.add(msg);
JOptionPane.showMessageDialog(null, panel);
}
}
The bidi text breaks into two lines. I am trying to write html to a JTree node so it will support multiple font families. I can't get it to work with a JLabel. I'm thinking I might need to paint it in a cell renderer. I was hoping to get the html to work. It would make things a lot easier.
Any suggestions?
=== Edit ===
When my display setting in Windows is at 125% it breaks the line; however, when I change my display setting in Windows to 100% it does not break the line. Running 1920 x 1080 display. Anyone have any ideas? Or, is anyone able to repeat the breaking of the line?
=== Edit ===
Interestingly when I pass -Dsun.java2d.uiScale= with 1.0 or 2.0 it works. When I use 3.0, 4.0, 1.25 or 1.5 or 0.8 it does not work.
Well, I submitted a bug report. I managed to find a workaround that seems to work for all Windows Display Scale's but it is quite a hacky workaround. But it will be sufficient until the bug is fixed.
Here it is:
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.GridLayout;
import java.awt.font.FontRenderContext;
public class Main
{
private static final String TEXT =
"\u05D0\u05B2\u05DC\u05B5\u05D9\u05DB\u05B6\u05DD";
public static void main(String[] args)
{
JLabel label = new JLabel();
Graphics2D g2 = (Graphics2D)new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontRenderContext frc = g2.getFontRenderContext();
String[] split = TEXT.split("");
double width = 0;
for(String chr : split)
width += label.getFont().getStringBounds(chr, frc).getWidth();
double scale = Toolkit.getDefaultToolkit().getScreenResolution()/96.0;
String html = "<html><p style=\"width:"+Math.ceil(width/scale)+"px;\">" + TEXT + "</p></html>";
label.setText(html);
JLabel msg = new JLabel("The text should at least go out to here.");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(label);
panel.add(msg);
JOptionPane.showMessageDialog(null, panel);
}
}
Note: If I don't get the individual character lengths then the width doesn't come out right in a JTree node. Also, for some reason when I use bidi text the resultant width of the nodes JLabel is about twice the width of the text. Any shorter and it does not display correctly. However, the width is correct with Non-Bidirectional text. Also, width: does not work with span tag, it only works with p tag
I want to use a TitledBorder around a JTextField without it taking up too much vertical space.
In the top it applies way more spacing for title font than is needed. Also in the bottom there's a whopping 4 pixels I can't use.
This occurs only on Windows; on Mac OSX the example below looks fine while on W10 the JTextField content is horribly cropped.
Can I reduce this in any way?
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MoreSpace {
static public void main(String args[]) {
EmptyBorder eb = new EmptyBorder(0, 0, 0, 0);
TitledBorder tb = new TitledBorder(eb, "Title");
Font font = new Font("dialog", Font.BOLD, 10);
tb.setTitleFont(font);
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(300,26));
textField.setBorder(tb);
textField.setText("I cant breathe in here");
JOptionPane.showMessageDialog(null, textField, "",JOptionPane.PLAIN_MESSAGE);
}
}
Create a custom TitledBorder class(from package javax.swing.border) and reduce the maximum EDGE_SPACING as desired.
// Space between the border and the component's edge
static protected final int EDGE_SPACING = 2;
this means 2 pixels above and below as padding by default for the TitledBorder. This should explain the 4 pixels you are seeing.
Setting EDGE_SPACING to 0 will do what you are looking for. :)
I wanted to create JButton to change color every time it is clicked but it doesn't change after second click.
It is strange because with Random().nextInt(250) instead of i it works.
What could be the problem?
Here's the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout(100, 100));
JButton l = new JButton("Hello");
l.setBackground(new Color(245, 12, 53));
jp.add(l, BorderLayout.EAST);
jf.add(jp);
jf.setSize(200, 200);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
l.addActionListener(new ActionListener() {
Integer i = new Integer(0);
Color c = new Color(150, 10, i);
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (i < 200) {
i += 50;
c = new Color(150, 10, i);
l.setBackground(c);
} else
i = 0;
}
});
}
}
I debugged your code and saw that the value of c change, every time I click the button. The first value is (r=150,g=10,b=50), then turns into (r=150,g=10,b=100), then (r=150,g=10,b=150) etc.
This means that the color is indeed changing. It's just that the difference is too small for you to notice.
So why does random.nextInt work?
With a random value in the blue component. The value can jump very suddenly from 0 to 200. The color difference is so large that your eyes can see it. But with a gradual change of 50 every time, you only notice it the first time.
Just test it with new Color(0, 0, i). I think that will make a bigger difference. It will go from black to blue!
Works fine for me.
Although the code should probably be something like:
if (i < 200)
i += 50;
else
i = 0;
c = new Color(150, 10, i);
l.setBackground(c);
Otherwise there will be one click that doesn't change the color.
You may want to consider using HSL Color this will allow you to change the Color in a more meaningful way by either changing the hue of the Color or shade/tone of the Color.
I am creating a java GUI which is a fortune teller. The GUI will spit out one of twelve fortunes every time you click the "get my fortune" button, the strings will never repeat back to back, can can repeat later after other strings have gone before it. I have made already for the most part. But now I am having some trouble creating the while loops to display the strings without repeating. I have looked at my book which didn't really help. If you guys could point me in the right direction,it would be much appreciated. Thanks!
I entered all of the code so you can see the variables used. But my question starts at class RndButtonListener.
package FortuneTellerRunner;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* #author a3cal_000
*/
class FortuneTellerFrame extends JFrame
{
final private JPanel mainPnl, titlePnl, displayPnl, buttonPnl, imagePnl;
final private JButton quitBtn, rndBtn;
final private JLabel titleLbl, iconLbl;
final private JTextArea displayTa;
final private JScrollPane scroller;
public String[] fortune = new String [12];
int newIndex, oldIndex;
private static final int HEIGHT = 250;
private static final int WIDTH = 450;
public FortuneTellerFrame()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
displayPnl = new JPanel();
buttonPnl = new JPanel();
titlePnl = new JPanel();
ImageIcon icon = new ImageIcon("FortuneTellerIcon.JPEG");
iconLbl = new JLabel(icon);
titleLbl = new JLabel("Fortune Teller!");
displayTa = new JTextArea();
imagePnl = new JPanel();
scroller = new JScrollPane();
// Create the layout of the title panel
titlePnl.setLayout(new GridLayout(2,1));
add(mainPnl);
// Set the label to the panel.
titlePnl.add(titleLbl);
titlePnl.add(iconLbl);
// add the panel to the main panel.
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scroller, BorderLayout.CENTER);
mainPnl.add(displayTa, BorderLayout.CENTER);
// Create the "Get my fortune button.
rndBtn = new JButton("Get My Fortune!");
quitBtn = new JButton("Quit");
// Add the buttons to the buttonPnl in grid layout.
buttonPnl.add(rndBtn);
buttonPnl.add(quitBtn);
// Create the grid layout for the button panel.
buttonPnl.setLayout( new GridLayout(1, 2));
// Add the button panel to the grid layout, South.
mainPnl.add(buttonPnl, BorderLayout.SOUTH);
ActionListener listener = new RndButtonListener();
rndBtn.addActionListener(listener);
quitBtn.addActionListener(listener);
}
class RndButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
fortune[0] = "He who throws dirt is losing ground.";
fortune[1] = "You will find the love of your life in food.";
fortune[2] = "Do or do not, there is no try.";
fortune[3] = "Tomorrow is a better day to try anything of importance.";
fortune[4] = "Life's not about how hard you can hit, but how hard you can get hit and keep moving forward.";
fortune[5] = "You can't be late until you show up.";
fortune[6] = "If you think things can't get worse it's probably only because you lack sufficent imagination.";
fortune[7] = "If youre at the top it means you have further to fall.";
fortune[8] = "Even in last place, youre still in the race.";
fortune[9] = "The road to riches is paved on the failures of others.";
fortune[10] = "If you feel like your going no where, get off the treadmill.";
fortune[11] = "Thinking about going to the gym is just as good as going.";
Random rnd = new Random(fortune.length);
do
{
newIndex = rnd.nextInt(fortune.length);
}
while(newIndex == oldIndex);
do
{
System.out.println(fortune[newIndex]);
displayTa.append(fortune[newIndex] + "||");
displayTa.updateUI();
mainPnl.updateUI();
oldIndex = newIndex;
}
while(newIndex != oldIndex);
class QuitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
}
}
}
}
The basic problem is you are re-creating the Random with the same seed each time, which is generally creating the same random sequence over and over again.
Instead try using...
do {
newIndex = (int) Math.round(Math.random() * (fortune.length - 1));
} while (newIndex == oldIndex);
You also don't need the second loop, it's just clutter that confuses the situation.
You may also find that...
displayTa.append(fortune[newIndex] + "\n");
produces nicer output (IMHO)
You may also wish to take a look at How to use Scroll Panes
Your program run fine, but this is a problem, fortune.length is a random seed which return me only 6 and 8 when I later called Random.nextInt().
Random rnd = new Random(fortune.length);
Do it this way
Random rnd = new Random();
and also consider the formatting solution given by MadProgrammer.
Random() gives you same number pattern. Try Random(System.currentTimeMillis()). It uses current time as seed, so you can get real random numbers.
I did something similar to this just today, so let's see if I can remember... I made an ArrayList of type int of how many items I had (fortunes)
ArrayList<Integer> fortuneSeq = new ArrayList<Integer>();
Then add in some numbers starting from 0 to code for the fortunes.
for(int i = 0; i < fortune.length; i++) {
fortuneSeq.add(i);
}
Then I used the shuffle() method from the Collections class to randomize the list.
Collections.shuffle(fortuneSeq);
After that, just loop through to access the fortunes.
for(int i = 0; i < fortune.length; i++) {
System.out.println(fortune[fortuneSeq.get(i)]);
//...
}
Edit: Silly autocorrect, you don't like programmers.
Edit: Fixed some furtunes instead of fortunes and fixed println statement.
I had my layout perfect until I couldn't figure out how to make drag and drop work. So to make the coding easier, I switched my labels on the bottom right side of my program to buttons to allow single clicking to generate an object in the main panel.
Now that I switched them, using BoxLayout, the buttons are not able to be sized for the image to fit perfectly in them, leaving edge space as seen in the photo. I also have a horizontal scroll bar now which I didn't have before with the labels.
I have tried several different layouts to try and fix the size of these buttons, but I can't get things to work right. I just need a vertical scroll bar and I want the buttons to be the exact size of the images, like they are in the panel above them. I tried setting the layout to null like I have in all the other panels and using the setBounds() method and that works perfectly for placement, but then the scroll bar disappears and won't scroll.
Anyone have any suggestions?
Edit: Here is what happens when I use the null layout.
I'd really recommend that you use GridBag layout if you're using swing. The other layouts leave a lot to be desired. It is all a matter of preference and you can lay it out manually if you want--there's no right answer.
The reason I prefer GridBag (or MigLayout--to each their own) is that you have a concept of preferred size for the component and the concept of fills. It has been a while since I coded up Swing (and I'll try to keep it that way!) but you're basically looking for something like:
{
//Pseudo Code, I'd have to go read the API again, I wrote a set of utilities so I wouldn't have to think about it.
GridBagConstraints constraints = ....;
constraints.weightX = 1.0; //fill the area by X
constraints.weightY = 1.0; //fill by Y
constraints.fill = GridBagConstraints.BOTH; //or one...
component.setPreferredSize(image.size());
layout.add(component, constraints);
}
Basically what you're doing is saying "use my preferred size as a minimum" but fill based on these rules.
The alternative--which doesn't use a layout is simply position the components yourself (there's absolutely nothing wrong with this).
{
JPanel panel =...;
panel.setLayout(null);
...
myButton3.setX(0);
myButton3.setY(2 * buttonHeight); //third button
myButton.setSize(myButton.getPreferredSize()); //which I assume you set
...
panel.add(myButton3);
...
}
Anyhow, there's a lot of options. Don't feel like you need to use a layout, write your own. You should care about these things and make it work but you shouldn't suffer. A layout is generally very simple to implement and you shouldn't be afraid to walk away from this.
All that said, GridBag will do what you want. Alternatively, Mig is great and has some nice GUI editors.
UPDATE -> -------------------------------
Here's a concise example--I sincerely do not advocate this style of programming, I just didn't want class spam for the example.
package _tests;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Grids extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String ... args)
{
new Grids().setVisible(true);
}
public Grids()
{
//Null layout example
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250, 300);
setMinimumSize(new Dimension(285, 300)); //Windows 8 ~ border size + scrollbar
setTitle("Test layouts");
JPanel scrollTarget = new JPanel()
{
private static final long serialVersionUID = 1L;
{
setSize(250, 1000);
setPreferredSize(new Dimension(250, 1000));
//setLayout(null); -- uncomment for absolute
setLayout(new GridBagLayout());
int lastX = 0;
int lastY = 0;
for(int i = 0; i < 5; i++)
{
final String label = "Button " + i;
JButton tmp = new JButton()
{
private static final long serialVersionUID = 1L;
{
setText(label);
setPreferredSize(new Dimension(250, 200)); //Preferred
}
};
tmp.setSize(tmp.getPreferredSize()); //What you're layout usually does..
//add(tmp);
//tmp.setLocation(lastX, lastY);
//lastY += tmp.getHeight();
add(tmp, getButtonConstraint(0, i));
}
}
};
add(new JScrollPane(scrollTarget));
}
private GridBagConstraints getButtonConstraint(int x, int y)
{
GridBagConstraints tmp = new GridBagConstraints();
tmp.fill = GridBagConstraints.BOTH;
tmp.weightx = 1.0;
tmp.weighty = 1.0;
tmp.gridx = x;
tmp.gridy = y;
tmp.anchor = GridBagConstraints.NORTHEAST;
return tmp;
}
}