label.setText is not working - java

I have this program that should replace the label "MALE" or "FEMALE" depending on which the chooser selects, but when I tried to run it the "setText" wouldn't work.
`import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hue implements ItemListener
{
JFrame frame= new JFrame("Demo");
Container content;
JPanel panel= new JPanel();
JLabel label = new JLabel("[LABEL]");
JCheckBox box= new JCheckBox("With Gender");
JRadioButton male= new JRadioButton("Male");
JRadioButton female= new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
JTextField field = new JTextField(100);
public void launchFrame()
{
panel.add(label);
panel.add(box);
panel.add(male);
panel.add(female);
bg.add(male);
bg.add(female);
box.addItemListener(this);
male.addItemListener(this);
female.addItemListener(this);
panel.add(field);
panel.setLayout(null);
label.setBounds(5,0, 100, 20);
box.setBounds(10,15, 100, 20);
male.setBounds(20, 35, 100, 20);
female.setBounds(20, 55, 100, 20);
field.setBounds(15, 80, 250, 100);
field.setHorizontalAlignment(JTextField.LEFT);
male.setEnabled(false);
female.setEnabled(false);
field.setEnabled(false);
content= frame.getContentPane();
content.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItem() == box)
{
if (!box.isSelected())
{
male.setEnabled(false);
female.setEnabled(false);
field.setEnabled(false);
}
else if (box.isSelected())
{
male.setEnabled(true);
female.setEnabled(true);
field.setEnabled(true);
{
if (e.getItem() == male)
{
label.setText("Male");
}
else if (e.getItem() == female)
{
label.setText("Female");
}
}
}
}
}
public static void main(String args[])
{
Hue hw= new Hue();
hw.launchFrame();
}
}`

public void itemStateChanged(ItemEvent e)
{
if (e.getItem() == box)
{
if (!box.isSelected())
{
male.setEnabled(false);
female.setEnabled(false);
field.setEnabled(false);
}
else if (box.isSelected())
{
male.setEnabled(true);
female.setEnabled(true);
field.setEnabled(true);
}
}
else if (e.getItem() == male)
{
label.setText("Male");
}
else if (e.getItem() == female)
{
label.setText("Female");
}
}
according to your code if (e.getItem() == box) then never happen if (e.getItem() == male) or if (e.getItem() == female)

Related

Change the background in JFrame

I'm getting the Co-ordinates of the mouse on the window , I'm also changing the background color using KeyListner
Letter B for Black
W for White
G for green
.......
but when I change the background color , only the background of the label is changing and not the entire window ,what I need is to change the entire window and not just the label , any ideas why is that ?
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class mouseCrd extends JFrame {
private JPanel contentPane;
public mouseCrd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 650);
setTitle("Mouse co-ordinates");
setVisible(true);
setResizable(false);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 650, 650);
contentPane.add(panel);
JLabel label = new JLabel(".................");
label.setFont(new Font("Tahoma", Font.BOLD, 30));
panel.add(label);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
label.setText("X = "+ e.getX()+" ; Y = "+e.getY());
}
});
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
int clr = e.getKeyChar();
if(clr==KeyEvent.VK_B)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.BLACK);
add(label);
}
else if(clr==KeyEvent.VK_W)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.WHITE);
add(label);
}
else if(clr==KeyEvent.VK_R)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.RED);
add(label);
}
else if(clr==KeyEvent.VK_O)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.ORANGE);
add(label);
}
else if(clr==KeyEvent.VK_G)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.GREEN);
add(label);
}
}
});
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable()
{
public void run() {
try {
mouseCrd frame = new mouseCrd();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
if you want to change the background of your aplication, you have to change the background of the JPane that contains all your components, you are doing:
label.setBackground(Color.ORANGE);
which is okay if you want to change the background of the label, this is explicit on the instruction, i did some changes in your code, look at this:
public class mouseCrd extends JFrame {
public mouseCrd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 650);
setTitle("Mouse co-ordinates");
setVisible(true);
setResizable(false);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 650, 650);
add(panel);
JLabel label = new JLabel(".................");
label.setFont(new Font("Tahoma", Font.BOLD, 30));
panel.add(label);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
label.setText("X = "+ e.getX()+" ; Y = "+e.getY());
}
});
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
char keyPressed = e.getKeyChar();
if(keyPressed=='b')
{
System.out.println("B is pressed");
panel.setBackground(Color.BLACK);
}
else if(keyPressed=='w')
{
System.out.println("W is pressed");
panel.setBackground(Color.WHITE);
}
else if(keyPressed=='r')
{
System.out.println("R is pressed");
panel.setBackground(Color.RED);
}
else if(keyPressed=='o')
{
System.out.println("O is pressed");
panel.setBackground(Color.ORANGE);
}
else if(keyPressed=='g')
{
System.out.println("G is pressed");
panel.setBackground(Color.GREEN);
}
}
});
}
public static void main(String[] args){
new mouseCrd();
}
}
I removed the contentPane because it was useless an then in the key released, i just change the backgound of the panel JPane with the method setBackground(), and i removed the use of the KeyEvent constants because is easier to use chars
I know what you already get the needed answer, but I would like to recommend you the Oracle Documentation regarding Swing and GUI's.
Its begginer friendly 😁 and it stress exactly what you need to know to have good fundaments.
https://docs.oracle.com/javase/tutorial/uiswing/index.html

Dispose() only working once

My problem is that I have a class that when the user types out the text displayed dispose() is called, which works the first time but if you don't close the program and open it again, dispose() is called, but doesn't do anything which breaks the program.
public class TypeMenu extends JDialog {
protected final JPanel contentPanel = new JPanel();
protected static JTextField inputTxtField;
protected static JTextField textField;
protected static JTextField introTxtField;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
* #param introTxtField2
* #param textField2
* #param inputTxtField2
*/
public TypeMenu(JTextField inputTxtField2, JTextField introTxtField2, JTextField textField2) {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getTextField());
contentPanel.add(getIntroTxtField());
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
protected JTextField getInputTxtField() {
if (inputTxtField == null) {
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
protected JTextField getIntroTxtField() {
if (introTxtField == null) {
introTxtField = new JTextField();
introTxtField.setHorizontalAlignment(SwingConstants.CENTER);
introTxtField.setFont(new Font("Tahoma", Font.BOLD, 15));
introTxtField.setText("Easy Mode");
introTxtField.setEditable(false);
introTxtField.setBounds(56, 11, 314, 29);
introTxtField.setColumns(10);
}
return introTxtField;
}
private JTextField getTextField() {
if (textField == null) {
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setFont(new Font("Monospaced", Font.BOLD, 20));
textField.setBounds(10, 51, 414, 40);
}
return textField;
}
}
This is one of the child classes
public class Easy extends TypeMenu {
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Easy() {
super(inputTxtField, introTxtField, textField);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getIntroTxtField());
getString();
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
textField.selectAll();
}
}
private void getString() {
String str = textField.getText();
if (str.equals("")) {
String generator = StringGenerator.medium();
String nStr = "" + generator;
textField.setText(nStr);
}
}
}
The code that calls this class
public class StartMenu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField introTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
StartMenu dialog = new StartMenu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates the dialog and creates the buttons that take the user to each variation of the game when pressed.
*/
public StartMenu() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
introTxt = new JTextField();
introTxt.setFont(new Font("Tahoma", Font.BOLD, 12));
introTxt.setHorizontalAlignment(SwingConstants.CENTER);
introTxt.setText("Start Menu\r\n");
introTxt.setEditable(false);
introTxt.setBounds(75, 11, 276, 20);
contentPanel.add(introTxt);
introTxt.setColumns(10);
}
{
JButton btnEasyButton = new JButton("Easy Mode");
btnEasyButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
new Easy().setVisible(true);
}
});
btnEasyButton.setBounds(141, 42, 140, 23);
contentPanel.add(btnEasyButton);
}
{
JButton btnMediumButton = new JButton("Medium Mode");
btnMediumButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Medium().setVisible(true);
}
});
btnMediumButton.setBounds(141, 81, 140, 23);
contentPanel.add(btnMediumButton);
}
{
JButton btnHardButton = new JButton("Hard Mode");
btnHardButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Hard().setVisible(true);
}
});
btnHardButton.setBounds(141, 120, 140, 23);
contentPanel.add(btnHardButton);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
}
your text field is static so he will have only one instance across the application.
so in the method getIntroTxtField() you have if statement that says :
if (introTxtField == null)
in the first time this condition is true but when you create the new instance this condition is false because the instance of the static field is all ready created in the first and you are adding the key listener inside the condition
so the action listener will be added only in the first creation.
if you need to keep the static because you need in other class you need to remove the == null
protected JTextField getInputTxtField() {
inputTxtField = null;
{
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
or remove the static from you field declaration static is used in
shared instace only when you are using only one instace across the
application like sessionFactory or any thing that need to be created
only one time.
you code is a lot to read but i think your problem is that you are calling the dispose() of the wrong object. may you are calling it always for the first object and you are creating a new one, and the dispose of this new dialog will never be call,Check you code may be you are creating many object and the dispose() is not called at all. make sure that you have full control of your objects instance to know if you are diposing the wanted dialog.

Java- disable buttons on one jpanel with another.

So my goal is to disable buttons on lets say panel B depending one what button they press on panel A. so below I have 2 combo boxes that id like to be able to enable or disable base on the buttons pressed on the first panel. Ive tried googling this problem but I'm new to java so its pretty rough going so far.
heres my sample code of what I'm trying to do.
package pack2;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class tessst {
public boolean enableChk1;
public boolean enableChk2;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tessst window = new tessst();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public tessst() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
frame.getContentPane().add(panel, "name_15095567731094");
panel.setLayout(null);
final JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_15101078033315");
panel_1.setLayout(null);
JButton select2 = new JButton("2 boxes");
select2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
enableChk2 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select2.setBounds(276, 101, 89, 23);
panel.add(select2);
JButton select1 = new JButton("1 boxes");
select1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select1.setBounds(59, 101, 89, 23);
panel.add(select1);
JButton select0 = new JButton("no boxes");
select0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = false;
enableChk2 = false;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select0.setBounds(166, 169, 89, 23);
panel.add(select0);
JComboBox comboBox = new JComboBox();
comboBox.setEnabled(enableChk1);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox.setBounds(52, 100, 61, 20);
panel_1.add(comboBox);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setEnabled(enableChk2);
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox_1.setBounds(265, 100, 79, 20);
panel_1.add(comboBox_1);
JButton back = new JButton("go back");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(true);
panel_1.setVisible(false);
}
});
back.setBounds(10, 227, 89, 23);
panel_1.add(back);
}
}
I needed to declare my comboBox's above my buttons then declare them as final.
here are the changes
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class tessst {
public boolean enableChk1;
public boolean enableChk2;
JComboBox comboBox;
JComboBox comboBox_1;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tessst window = new tessst();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public tessst() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
frame.getContentPane().add(panel, "name_15095567731094");
panel.setLayout(null);
final JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_15101078033315");
panel_1.setLayout(null);
JButton select2 = new JButton("2 boxes");
select2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
enableChk2 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(true);
comboBox_1.setEnabled(true);
}
});
select2.setBounds(276, 101, 89, 23);
panel.add(select2);
JButton select1 = new JButton("1 boxes");
select1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(true);
}
});
select1.setBounds(59, 101, 89, 23);
panel.add(select1);
JButton select0 = new JButton("no boxes");
select0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = false;
enableChk2 = false;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(false);
comboBox_1.setEnabled(false);
}
});
select0.setBounds(166, 169, 89, 23);
panel.add(select0);
comboBox = new JComboBox();
comboBox.setEnabled(enableChk1);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox.setBounds(52, 100, 61, 20);
panel_1.add(comboBox);
comboBox_1 = new JComboBox();
comboBox_1.setEnabled(enableChk2);
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox_1.setBounds(265, 100, 79, 20);
panel_1.add(comboBox_1);
JButton back = new JButton("go back");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(true);
panel_1.setVisible(false);
}
});
back.setBounds(10, 227, 89, 23);
panel_1.add(back);
}
}
replace your code with this one. Its working Fine. Hope you are expecting this thing.

Eclipse Why Does It Create An Extra Window

I'm trying to make a launcher. It works fine, except that when I press the 'Options' button it makes a little window in the corner. How do I prevent this?
The Launcher picture:
(source: gyazo.com)
And now with the option frame running:
(source: gyazo.com)
You can see the little window in the corner. It does not disappear when the Option frame does.
The Main File:
package ca.sidez.Launcher;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ca.sidez.Main.Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Main {
private Game ga;
public static int game = 0;
public static int isUpdate = 0;
public static int BigVersion = 00;
public static String Version = "04";
public static String Potenic_Version = "1.6a";
public static String DirtLife_Version = "0.1";
public static int lang = 0;
private static void createWindow() {
Font font;
Font font2;
final JFrame frame = new JFrame("Nic Launcher " + Version);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(820, 640);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(false);
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
font = new Font("Times New Roman", Font.PLAIN, 28);
font2 = new Font("Arial", Font.BOLD, 14);
jep.setContentType("text/html");
jep.setText("<html> Loading... </html>");
jep.setBorder(new EmptyBorder(0, 0, 0, 0));
try {
jep.setPage("http://potenic.tumblr.com/");
} catch(IOException e) {
jep.setContentType("text/html");
jep.setText("<html>Could not load, Check Your Connection</html>");
}
JScrollPane scrollPane = new JScrollPane(jep);
scrollPane.setBackground(Color.BLACK);
scrollPane.setSize(640, 480);
scrollPane.setLocation(0, 0);
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
final JRadioButton potenic = new JRadioButton("Potenic: " + Potenic_Version);
final JRadioButton Launcher = new JRadioButton("Launcher");
potenic.setBounds(675, 150, 200, 55);
Launcher.setBounds(675, 100, 200, 55);
ButtonGroup bg = new ButtonGroup();
bg.add(potenic);
bg.add(Launcher);
Launcher.setVisible(false);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(potenic.isSelected()) {
game = 1;
Launcher.setVisible(true);
} else {
game = 0;
Launcher.setVisible(false);
}
}
};
potenic.addActionListener(al);
Launcher.addActionListener(al);
//Enslish
if(lang == 0) {
a JButton launchButton = new JButton("Play");
launchButton.setBounds(50, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Check Update");
updateButton.setBounds(250, 530, 250, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("Update");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("Options");
OptionsButton.setBounds(550, 530, 150, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
Working frame3 = new Working();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
//Svenska
if(lang == 1) {
JButton launchButton = new JButton("Spela");
launchButton.setBounds(30, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Titta Efter Uppdateringar");
updateButton.setBounds(215, 530, 350, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("Uppdatera");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("InstÀllningar");
OptionsButton.setBounds(600, 530, 200, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
LauncherOptions frame3 = new LauncherOptions();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
//Suomi
if(lang == 2) {
JButton launchButton = new JButton("Pelaa");
launchButton.setBounds(50, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Tarkista pÀivitys");
updateButton.setBounds(250, 530, 250, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("pÀivittÀÀ");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("Asetukset");
OptionsButton.setBounds(550, 530, 150, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
LauncherOptions frame3 = new LauncherOptions();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
frame.add(scrollPane);
frame.add(potenic);
frame.add(Launcher);
frame.setVisible(true);
}
public static void main(String[] args) {
new LoadLang();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createWindow();
}
});
}
}
The Option Code:
package ca.sidez.Launcher;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ca.sidez.Main.Game;
import ca.sidez.Main.GamePanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Working extends JFrame {
private Game ga;
public static int game;
Font font;
Font font2;
public Working() {
final JFrame frame = new JFrame("Working In Progress");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
font = new Font("Times New Roman", Font.PLAIN, 28);
font2 = new Font("Arial", Font.BOLD, 14);
JLabel working = new JLabel("Working In Progress");
frame.add(working);
//English
if(Main.lang == 0) {
JButton launchButton = new JButton("Back");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
frame.add(launchButton);
}
//Svenska
if(Main.lang == 1) {
JButton launchButton = new JButton("GĂ„ Tillbaks");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.add(launchButton);
}
//Suomi
if(Main.lang == 2) {
JButton launchButton = new JButton("Takaisin");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.add(launchButton);
}
frame.setVisible(true);
}
}
In your OptionsButton.addActionListener (pretty bad name for a variable, should not start with a capital letter), you create a Working object that IS a JFrame, and it's showed up.
But, in your Working class (realy bad name), that extends JFrame you work and show a final JFrame frame attribute.
That's why there is two windows (JFrame) showing up.
Delete that final JFrame frame in Working, and just use this !

Unresponsive JTextField

In my application I need to get a response between 1 and 9 from the user. I am using a JTextField to get the input. However when a button is pressed the user the JTextField becomes unresponsive. Here is a stripped down example of the problem:
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class InputTest {
private JTextField inputBox;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new InputTest();
}
});
} // end main()
public InputTest() { // constructor
JFrame f = new JFrame("Input Test");
f.setBounds(100, 100, 450, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
JLabel label = new JLabel("Enter an integer:");
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
label.setBounds(109, 120, 109, 30);
f.add(label);
inputBox = new JTextField();
inputBox.setBounds(259, 120, 30, 30);
f.add(inputBox);
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed");
}
});
button.setMnemonic('B');
button.setBounds(166, 198, 78, 23);
f.add(button);
f.setVisible(true);
inputBox.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (c < '!' || e.getModifiers() > 0)
return;
if (c < '1' || c > '9') {
JOptionPane.showMessageDialog(null, c
+ " is not an integer");
// inputBox.setText("error");
System.out.println("You typed a non-integer");
} else
System.out.println("You typed " + c);
inputBox.setText(null);
}
});
} // end constructor
} // end class
You should almost never use a KeyListener with a Swing JTextComponent such as a JTextField, for example what happens when the user tries to copy and paste in their input? Instead consider using either a JFormattedTextField, or a JTextField with a DocumentFilter, or my choice -- a JSpinner.
import java.awt.event.ActionEvent;
import javax.swing.*;
public class InputTest2 extends JPanel {
private JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 1, 9, 1));
private JButton getSelectionButton = new JButton(new GetSelectionAction("Get Selection"));
public InputTest2() {
add(spinner);
add(getSelectionButton);
}
private static void createAndShowGui() {
InputTest2 mainPanel = new InputTest2();
JFrame frame = new JFrame("InputTest2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class GetSelectionAction extends AbstractAction {
public GetSelectionAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent evt) {
int value = ((Integer)spinner.getValue()).intValue();
System.out.println("You've selected " + value);
}
}
}

Categories

Resources