Java GUI doesn't run smoothly - java

I'm trying to write a Java GUI. I've been having problems where the program doesn't run smoothly; for example, the buttons take a very long time to appear and appear in wrong places. Below is my code and how the button looks like. Shouldn't the "start" be placed in a button? Is there a problem with my code because this has been happening for quite a while.
import java.util.Random;
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class QuizGen {
public static void main(String[] args) {
JFrame frame = new JFrame("Quiz Generator");
JPanel panel = new JPanel();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton start = new JButton("Start");
start.setBounds(0, 0, 100, 100);
frame.add(panel);
frame.add(start);
frame.setVisible(true);
}
}
enter image description here

Related

How can I split a JFrame in half so that I can have 2 JPanels, 1 that is a map editor and the other that is a tile selector for the map?

I can't seem to get the grid displayed on the first panel for some reason. If anyone knows why I would love to hear that. Also I would like the 1st panel(on the left) to be wider than the panel on the left. I know I can't do that without gridLayout, but I can't seem to find another way. Right now I would like to be able to choose a tile from the panel on the right. I would then have a little window that contains the tile I just picked and I would then just left click to add the tile to the map or right click to remove it. If anyone has any idea how I can do this please help me out. I've gotten started, but I can't seem to get the darn thing to even display the map.
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Grid extends JFrame {
JFrame window = new JFrame("Map Editor");
JPanel main = new JPanel();
JPanel tilePicker = new JPanel();
JPanel gridHolder = new JPanel();
JPanel grid = new JPanel();
public void createGrid() {
ImageIcon dirt = new ImageIcon("C:/Users/Andreas.ANDREAS/Desktop/dirt.png");
grid.setLayout(new GridLayout(11,11));
for (int i = 0; i < 11; i++) {
for (int n = 0; n < 11; n++) {
grid.add(new JLabel(dirt));
}
}
}
public Grid() {
// window = this;
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(100,100,500,500);
window.setLayout(new BorderLayout());
window.setVisible(true);
gridHolder.add(grid);
tilePicker.add(new JLabel("TEST"));
tilePicker.setBackground(Color.BLACK);
main.setLayout(new GridLayout(1,2));
main.add(gridHolder);
main.add(tilePicker);
window.add(main);
}
public static void main(String args[]) {
new Grid();
}
}
When I first made this I wanted to see if the grid would at least display in the JFrame window and it did without any problem. It filled the entire window. Here's the code for that part.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
public class test extends JPanel {
public static void main(String args[]) {
ImageIcon dirt = new ImageIcon("C:/Users/Andreas.ANDREAS/Desktop/dirt.png");
ImageIcon blackDefault = new ImageIcon("C:/Users/Andreas.ANDREAS/Desktop/black.png");
JLabel grid = new JLabel();
grid.setLayout(new GridLayout(11,11));
for (int i = 0; i < 11; i++) {
for (int n = 0; n < 11; n++) {
grid.add(new JLabel(dirt));
}
}
//creates the main JFrame/window
JFrame window = new JFrame("Map Editor");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(100,100,500,500);
window.setVisible(true);
window.setResizable(false);
window.add(grid);
grid.setVisible(true);
}
}
Use a JSplitPane.
One instance has one splitter (2 frames).
If You want to have more splitters - just add a new instance to another.
Add JSplitPane spl to frame.
Add Your components to spl.
Set bounds, splitter size, defaultPosition, whatever
run file
EDIT:
Grid(){
main.setBounds(0, 0, 500, 500);
}
createGrid(){
//Your loops
grid.setBounds(0,0,200,200);
}
Itworks for me (i se my icons on a left);

How to put JComboBox into an Applet

I have this code below. How do I make this JComboBOx run in an Applet? I'm trying to create an applet that allows the user to select a font and type in that font in a JTextArea. I have been searching for answers and made little progress. The error java.lang.reflect.InvocationTargetException keeps popping up. If you need more specifics please just comment.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Font_Chooser extends JApplet {
JLabel jlbPicture;
public Font_Chooser(){
// Create the combo box, and set first item as Default
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
final JComboBox comboTypesList = new JComboBox(names);
comboTypesList.setSelectedIndex(0);
comboTypesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox jcmbType = (JComboBox) e.getSource();
String cmbType = (String) jcmbType.getSelectedItem();
jlbPicture.setIcon(new ImageIcon(""
+ cmbType.trim().toLowerCase() + ".jpg"));
System.out.println(comboTypesList.getSelectedItem());
//Font myFont = new Font((String)comboTypesList.getSelectedItem(), Font.BOLD, 12);
}
});
// Set up the picture
jlbPicture = new JLabel(new ImageIcon(""
+ names[comboTypesList.getSelectedIndex()] + ".jpg"));
jlbPicture.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
jlbPicture.setPreferredSize(new Dimension(177, 122 + 10));
// Layout the demo
setLayout(new BorderLayout());
add(comboTypesList, BorderLayout.NORTH);
add(jlbPicture, BorderLayout.SOUTH);
}
public static void main(String s[]) {
JFrame frame = new JFrame("JComboBox Usage Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new Font_Chooser());
frame.pack();
frame.setVisible(true);
}
}

How can we put value on text field on output screen?

I want to put value in txtf1 at output screen and get it. How can we put value on text field on output screen?
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class demog extends JPanel implements ActionListener{
private TextField textf, txtf1;
public void jhand(){
textf = new TextField();
textf.setSize(40, 40);
textf.setText("20");
textf.setEditable(false);
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
//textf.setHorizontalAlignment(SwingConstants.CENTER);
textf.setLocation(15, 15);
//textf.addActionListener(this);
txtf1 = new TextField();
txtf1.setSize(40, 40);
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
//txtf1.setHorizontalAlignment(SwingConstants.CENTER);
txtf1.setLocation(50, 50);
JFrame frame = new JFrame("demo");
JPanel p = new JPanel();
p.setOpaque(true);
p.setBackground(Color.WHITE);
p.setLayout(null);
frame.setContentPane(p);
frame.setSize(500,500);
frame.setVisible(true);
p.add(textf);
p.add(txtf1);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
System.out.println(text);
}
public static void main(String... args){
demog g = new demog();
g.jhand();
}
}
You have to change some of your code in order to work. You had some problem in your code which I resolved them for you in the following code. See the comments to learn some in swing ;-)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
// Use upper Case in the start of you class names:
public class Demog extends JPanel implements ActionListener {
private JTextField textf, txtf1;
public Demog() {
jhand();
}
public void jhand() {
setLayout(new FlowLayout()); // Always set the layout before you add components
// you can use null layout, but you have to use setBounds() method
// for placing the components. For an advanced layout see the
// tutorials for GridBagLayout and mixing layouts with each other.
textf = new JTextField(); // Do not mix AWT component with
// Swing (J components. See the packages)
//textf.setSize(40, 40); // Use setPreferredSize instead
textf.setPreferredSize(new Dimension(40, 40));
textf.setText("20");
textf.setEditable(false); // Text fields are for getting data from user
// If you need to show something to user
// use JLabel instead.
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
add(textf);
txtf1 = new JTextField();
//txtf1.setSize(40, 40); Use setPreferredSize instead
txtf1.setPreferredSize(new Dimension(40, 40));
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
add(txtf1);
JButton b = new JButton("Click ME!");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
JOptionPane.showMessageDialog(Demog.this, "\"textf\" text is: "+text);
}
public static void main(String[] args) {
JFrame frame = new JFrame("demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Demog p = new Demog();
p.setBackground(Color.WHITE);
frame.setContentPane(p);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Good Luck.

Converter Program for Hex to Dec and Binary

Given my code below, I'm a little confused as to what I am missing in order to allow it to function so that when the user inputs their hexadecimal value with a maximum of five digits, they will get their binary equivalent and the decimal equivalent. I thought the code at the bottom automatically converts given a certain number in hex. Also would it be difficult to store the binary values in an array?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ConvertPanel {
public static void main(String[] args) {
JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NumberConverter());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NumberConverter extends JPanel {
private JLabel binaryLabel = new JLabel();
private JLabel totalone = new JLabel();
private JLabel totaltwo = new JLabel();
private JLabel decimalLabel = new JLabel();
private JTextField hexdecString = new JTextField();
private JButton convert;
public NumberConverter() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel converterName = new JLabel("Hexadecimal Input");
convert = new JButton ("Convert");
convert.addActionListener (new ButtonListener());
JPanel panelName = new JPanel(new GridLayout(2,2));
panelName.add(converterName);
panelName.add(hexdecString);
add(panelName, BorderLayout.NORTH);
add (convert);
JPanel totalPanel = new JPanel(new GridLayout(1,3));
totalPanel.add(new JLabel("Binary:"));
totalone = new JLabel("---- ---- ---- ---- ----");
totalPanel.add(totalone);
totalPanel.add(binaryLabel);
JPanel totalPanel2 = new JPanel(new GridLayout(2,3));
totalPanel2.add(new JLabel("Decimal:"));
totaltwo = new JLabel("------");
totalPanel2.add(totaltwo);
totalPanel2.add(decimalLabel);
JPanel south = new JPanel(new GridLayout(2,1));
south.add(totalPanel);
south.add(totalPanel2);
add(south, BorderLayout.SOUTH);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
Integer n = Integer.valueOf(hexdecString.getText(), 16);
decimalLabel.setText(String.valueOf(n));
binaryLabel.setText(Integer.toBinaryString(n));
} {
}
}
}

JFrame duplication on main screen

My code is duplicating the creation of the main screen when I hit the button of the search..the search event should only open one new frame with a textbox to type query. The creation of another frame causes this duplication or is this some sort of bug? I tryed to use the mainFrame to upload the date of the searchPane (p1 in the code) but when I do this, this solves my problem of the window, but the defaultCloseOperation() on the Search Windows causes the close of the entire program - how may I solve this situtation?
thanks in advance
SearchScreen
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchScreen extends MainScreen{
JButton btsearch;
JLabel lbsearch;
protected JTextField txtsearch;
JPanel p1;
protected JFrame searchFrame = new JFrame();
public SearchScreen(){
//Button Creation
btsearch= new JButton("Search");
//Label Creation
lbsearch= new JLabel("Type Keywords in english to be searched below:");
//TextBox
txtsearch= new JTextField();
//Pane Creation
p1=new JPanel();
p1.setBackground(Color.gray);
//Pane Components
p1.add(lbsearch);
p1.add(txtsearch);
p1.add(btsearch);
//JFrame Layout Setup
p1.setLayout(new GridLayout(3,3));
btsearch.setEnabled(true);
//Adding JPaneel
searchFrame.add(p1);
//JFrame Setup
searchFrame.setTitle("SHST");
searchFrame.setSize(400, 400);
searchFrame.setVisible(true);
searchFrame.setDefaultCloseOperation(1);
}
}
MainScreen
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class MainScreen implements ActionListener {
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
JPanel entrance = new JPanel();
JFrame mainFrame = new JFrame();
public MainScreen()
{
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Search");
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Request Query");
search.addActionListener(this);
//mainFrame Setup
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH);
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
public static void main (String[] args){
MainScreen m= new MainScreen();
}
}
Your issue is here
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
Using inheritance implementation is dangereous, a SearchScreen is a MainScreen then the constructor of this class is always called.

Categories