I apologise now if my terminology or turns of phrase aren't right, I'm new to swing. Also, apologise for the clunky and intelligent way I've tried to get this to work.
I am trying to create a single box that has a JTextArea inside it, and next this this area (still in the same box), has some buttons. However, all I've managed to do is make a box with the buttons in, and a separate box with a text area.
I have looked at many examples where people are trying to do similar things and I thought I managed to understand and implement it, but I haven't yet succeeded. If someone could point me at the part(s) of the code I'm misunderstanding I would be very grateful.
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.*;
public class GameGUI extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea(50, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
String action;
private static GUIClient client = new GUIClient();
JButton n = new JButton("N");
JButton e = new JButton("E");
JButton s = new JButton("S");
JButton w = new JButton("W");
JButton look = new JButton("LOOK");
JButton pickup = new JButton("PICKUP");
JButton hello = new JButton("HELLO");
JButton quit = new JButton("QUIT");
public GameGUI()
{
textArea.setEditable(false);
panel.add(new JScrollPane(textArea));
this.setBounds(300, 300, 750, 500);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
n.setBounds(225, 25, 50, 50);
e.setBounds(300, 100, 50, 50);
s.setBounds(225, 175, 50, 50);
w.setBounds(150, 100, 50, 50);
look.setBounds(50, 362, 100, 50);
pickup.setBounds(200, 362, 100, 50);
hello.setBounds(350, 362, 100, 50);
quit.setBounds(500, 362, 100, 50);
this.add(n);
this.add(e);
this.add(s);
this.add(w);
this.add(look);
this.add(pickup);
this.add(hello);
this.add(quit);
n.addActionListener(this);
e.addActionListener(this);
s.addActionListener(this);
w.addActionListener(this);
look.addActionListener(this);
pickup.addActionListener(this);
hello.addActionListener(this);
quit.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void displayInGUI(String fromServer)
{
textArea.append(fromServer);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
public void actionPerformed(ActionEvent f)
{
if(f.getSource()==n)
{
action = "MOVE N";
client.display(action);
}
else if(f.getSource()==e)
{
action = "MOVE E";
client.display(action);
}
else if(f.getSource()==s)
{
action = "MOVE S";
client.display(action);
}
else if(f.getSource()==w)
{
action = "MOVE W";
client.display(action);
}
else if(f.getSource()==look)
{
action = "LOOK";
client.display(action);
}
else if(f.getSource()==pickup)
{
action = "PICKUP";
client.display(action);
}
else if(f.getSource()==hello)
{
action = "HELLO";
client.display(action);
}
else if(f.getSource()==quit)
{
action = "QUIT";
client.display(action);
}
}
}
I've used this class by creating an instance of it in GUIClient (I've also created an instance of GUIClient in this class, GameGUI, which seems really ugly but it was the only way I could think to notice when the buttons were pressed).
So, if I want to run the game, I set up the server, then run GUIClient, which creates the two boxes. GUIGame then notices when the buttons are pressed and sends this info to the client which sends it to the server.
Again, I'm sorry this is so convoluted. I hope my explanation helped clear things up but if not just let me know.
Many thanks,
Elise.
I added a 'main()' method (and commented out the client parts) so I could run your program. I also set the Frame's size to 800x500 so it is visible. This is what I see:
The reason the text area is not visible is you did not add it to the frame. You will need to add the panel that contains the scrollpane that contains the textarea to the frame. You will also need to position and size it. For example:
panel.setBounds(400, 25, 200, 200);
this.add(panel);
This produces the following:
Related
Recently I've been trying to make a program that takes in a double in the form of a string. It then parses that to a double which goes to another class to be divided to a quarter or a half and then returns that output to a label.
I've been having an issue where when I click a button to actually submit what is inside the text field, the label doesn't change.
I've tried a lot of trial and error and I know I can change the text after doing new JLabel("test") for example. However, there seems to be an issue with my action listener for when the button is pushed. I can't tell if it's not seeing the button as being pushed.
NOTE: I am new to awt event things and swing as a whole, I usually operate just using the output terminal of visual studio code where it's just text and no graphics.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Dimension;
public class MoneySorterRunner {
private MoneySorter sorter = new MoneySorter();
private String input = "0";
private double money = Double.parseDouble(input);
private static JTextField typeHere = new JTextField();
///labels num1-3 are the labels being changed
private static JLabel num1 = new JLabel(new MoneySorterRunner().sorter.divQuarter(new MoneySorterRunner().money));
private static JLabel num2 = new JLabel(new MoneySorterRunner().sorter.divQuarter(new MoneySorterRunner().money));
private static JLabel num3 = new JLabel(new MoneySorterRunner().sorter.divHalf(new MoneySorterRunner().money));
public static void main(String args[]) {
JFrame frame = new JFrame("Money Calculator - v0.1a");
JPanel panel = new JPanel();
JButton doThing = new JButton("Do a Thing");
doThing.setActionCommand("Do a Thing");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 250);
frame.setLocation(200, 200);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
panel.setLayout(null);
frame.setVisible(true);
JLabel item1 = new JLabel("test");
JLabel item2 = new JLabel("test");
JLabel item3 = new JLabel("test");
item1.setFont(new Font("Arial", Font.PLAIN, 30));
item2.setFont(new Font("Arial", Font.PLAIN, 30));
item3.setFont(new Font("Arial", Font.PLAIN, 30));
num1.setFont(new Font("Arial", Font.PLAIN, 25));
num2.setFont(new Font("Arial", Font.PLAIN, 25));
num3.setFont(new Font("Arial", Font.PLAIN, 25));
Dimension size1 = item1.getPreferredSize();
Dimension size2 = item2.getPreferredSize();
Dimension size3 = item3.getPreferredSize();
panel.add(item1);
panel.add(item2);
panel.add(item3);
panel.add(num1);
panel.add(num2);
panel.add(num3);
panel.add(doThing);
panel.add(typeHere);
item1.setBounds(10, 10, size1.width + 3, size1.height);
item2.setBounds(190, 10, size2.width + 3, size2.height);
item3.setBounds(325, 10, size3.width + 3, size3.height);
num1.setBounds(50, 50, 50, 25);
num2.setBounds(200, 50, 50, 25);
num3.setBounds(350, 50, 50, 25);
doThing.setBounds(250, 150, 100, 25);
typeHere.setBounds(100, 150, 150, 25);
}
public void actionPerformed(ActionEvent event){
String check = event.getActionCommand();
if(check.equals("Do a Thing")){
input = typeHere.getText();
}
if(input != "0"){
num1.setText(sorter.divQuarter(money));
num2.setText(sorter.divQuarter(money));
num3.setText(sorter.divHalf(money));
}
}
}
For those who wanted the MoneySorter.java:
public MoneySorter(){
}
public String divQuarter(double moneyIn){
String answer = Double.toString(moneyIn);
return answer;
}
public String divHalf(double moneyIn){
String answer = Double.toString(moneyIn);
return answer;
}
}
I understand that your program is supposed to do the following.
User enters an amount of money in a JTextField and when she clicks on a JButton the JLabels show the entered amount in dollars, half-dollars and quarters (as per U.S. currency). My answer, below, is based on this understanding.
I don't know if making all the variables static is good or bad but I never use static class member variables in my Swing programs.
Here is my analysis of your code.
private double money = Double.parseDouble(input);
This line of code will be executed precisely once, when you launch class MoneySorterRunner. You want to do this every time the JButton is clicked, hence parsing the text entered into the JTextField should be performed in the actionPerformed method.
JPanel panel = new JPanel();
panel.setLayout(null);
It is almost never needed to set the layout manager to null. You can almost always find an appropriate layout manager or you can place one JPanel within another and use different layout managers for each JPanel in order to get the desired placement of components within the Swing application window.
JButton doThing = new JButton("Do a Thing");
doThing.setActionCommand("Do a Thing");
By default, the text of a JButton is also its action command so no need to explicitly set it.
frame.setLocation(200, 200);
frame.setLocationRelativeTo(null);
These are two different ways to set the location of the JFrame and they do not complement each other. Use one or the other, but not both.
frame.setVisible(true);
Only after you have created all the [GUI] components and added them to the JFrame should you make the JFrame visible. So this should be the last line of the code that creates your GUI.
doThing.setBounds(250, 150, 100, 25);
If you use a layout manager, you never need to call method setBounds.
if(input != "0"){
This is not the way to compare strings. Use method equals as you have done here
if(check.equals("Do a Thing")){
Here is my rewrite of your application. Note that since I could not find the code for class MoneySorter, in your question, I just created my own version of that class. The point is to show how to change the text of the JLabel after clicking on the JButton and not how to create the actual text to display.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
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;
import javax.swing.JTextField;
public class MoneySorterRunner implements ActionListener {
private MoneySorter sorter = new MoneySorter();
private JTextField typeHere = new JTextField();
private JLabel num1;
private JLabel num2;
private JLabel num3;
private void createAndShowGui() {
JFrame frame = new JFrame("Money Calculator - v0.1a");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(createLabels(), BorderLayout.PAGE_START);
frame.add(createForm(), BorderLayout.CENTER);
frame.setSize(550, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createForm() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
panel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
typeHere = new JTextField(10);
panel.add(typeHere);
JButton doThing = new JButton("Do a Thing");
doThing.addActionListener(this);
panel.add(doThing);
return panel;
}
private static JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setFont(new Font("Arial", Font.PLAIN, 25));
return label;
}
private JPanel createLabels() {
JPanel panel = new JPanel(new GridLayout());
num1 = createLabel("num1");
panel.add(num1);
num2 = createLabel("num2");
panel.add(num2);
num3 = createLabel("num3");
panel.add(num3);
return panel;
}
public static void main(String args[]) {
new MoneySorterRunner().createAndShowGui();
}
public void actionPerformed(ActionEvent event){
String check = event.getActionCommand();
if(check.equals("Do a Thing")){
String text = typeHere.getText();
if (!text.isEmpty()) {
double money = Double.parseDouble(text);
num1.setText(sorter.divQuarter(money));
num2.setText(sorter.divQuarter(money));
num3.setText(sorter.divHalf(money));
}
}
}
}
class MoneySorter {
public String divQuarter(double money) {
return "divQuarter(" + money + ")";
}
public String divHalf(double money) {
return "divHalf(" + money + ")";
}
}
This is how your GUI looked when I ran your original code (as posted in your question).
This is how the GUI looks when running the code in this answer.
After launching
After entering a value and clicking the JButton
It looks like you forgot to set the ActionListener. You should also change your method to another name, because your method has the same name as the actionPerformed of the ActionListener.
doThing.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new MoneySorterRunner().actionPerformedMethod(e);
}
});
You forgot to add the ActionListener.
In order to fix this, you need to do two things:
Add this statement to your code, preferably near where you create the button (so that it is easier to keep track). -
doThing.addActionListener(this);
When you write "public class", you also need this keyword: implements ActionListener - meaning your class (basically the first line) should look like:
public class MoneySorterRunner implements ActionListener
And that should make it work.
I am a beginner to java and stuck with the below issue.
The purpose is to display the login window when log out button is pressed.
First JFrame window displayed is "Plain" with 2 fields username and password(I will be adding the log in functionality later)
When I press the submit button the JFrame "NEw Window" is displayed with the button "LOGOUT"
What I would like to do is that when the "LOGOUT" is pressed the "NEw Window" should close and the "Plain" window should open.
Present issue: When "LOGOUT" button is pressed the "NEw Window" is opening up.
Please correct the code so that I get the desired functionality
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Test implements ActionListener{
JButton submit;
JFrame j;
JFrame jf;
public Test()
{
j = new JFrame("PLAIN");
j.setBounds(500,150,300,400);
j.setVisible(true);
j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
j.add(panel);
panel.setSize(50, 50);
panel.setLayout(null);
JLabel label = new JLabel("User Name");
label.setSize(10,10);
label.setBounds(100, 30, 400, 30);
panel.add(label);
JTextField username = new JTextField(10);
username.setSize(10,10);
username.setBounds(300, 30, 400, 30);
panel.add(username);
JLabel password= new JLabel("Password");
password.setBounds(100, 90, 400, 30);
panel.add(password);
JPasswordField pass = new JPasswordField(10);
pass.setBounds(300, 90, 400, 30);
panel.add(pass);
submit = new JButton("Submit");
submit.setSize(10, 10);
submit.setBounds(300, 160, 200, 40);
panel.add(submit);
submit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
j.setVisible(false);
jf = new JFrame("NEw Window");
jf.setVisible(true);
jf.setBounds(500,150,300,400);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
jf.add(panel2);
JButton logout = new JButton("LOGOUT");
logout.setBounds(100, 30, 400, 30);
panel2.add(logout);
logout.addActionListener(this);
jf.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
}
public void actionPerformed1(ActionEvent e1) {
jf.dispose();
j.setVisible(true);
}
public static void main(String args[])
{
new Test();
}
}
Some Points:
Call JFrame#setVisible() in the end after adding all the components.
Never use null layout at all instead use proper Layout Manager.
Read more A Visual Guide to Layout Managers where all the layout manger are described in detail along with sample code.
Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.
Read more
Why to use SwingUtilities.invokeLater in main method?
SwingUtilities.invokeLater
Try with WindowConstants.DISPOSE_ON_CLOSE instead of JFrame.EXIT_ON_CLOSE.
Just put j.setVisible(true) from Test() at the end, after adding all the components.
If you want to make a new form, don't do it in the same class where you already have one because it isn`t right.
Read the book named Clean code. You also have some errors in your code and useless.
I'm working on building a program that uses JFrame. What I want for my end result, is to implement an ActionListener which will remove labels when the user clicks a button. For example: when the user clicks the JButton, one of 5 labels is removed from the frame. When they click the button again, one of the remaining 4 labels is removed...and so on a so forth, until 0 labels remain. Technically, I have the program working as required however, I'm trying to see if there is a way to implement the ActionListener event via a loop as opposed to listing an if statement for each individual label. Thank you so much!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//calls for public class to inherit features of JFrame within Java
public class NoPurchaseReason extends JFrame implements ActionListener {
private int removeText = 0;
JButton btn = new JButton("Select");
JLabel lbl = new JLabel("Found better price");
JLabel lbl1 = new JLabel("Not as shown on website");
JLabel lbl2 = new JLabel("Wrong product");
JLabel lbl3 = new JLabel("Damaged upon delivery");
JLabel lbl4 = new JLabel("None of the above");
public static void main(String[] args) {
JFrame f = new NoPurchaseReason("Please tell us why you wish to return your purchase.");
f.setBounds(300, 100, 500, 500);
f.setVisible(true);
f.setBackground(Color.blue);
}
public NoPurchaseReason(String title) {
super(title);
setLayout(null);
lbl.setBounds(40, 40, 600, 40);
btn.setBounds(320, 10, 80, 20);
lbl.setBounds(100, 40, 100, 20);
lbl1.setBounds(100, 70, 100, 20);
lbl2.setBounds(100, 100, 150, 20);
lbl3.setBounds(100, 130, 100, 20);
lbl4.setBounds(100, 160, 100, 20);
add(btn);
add(lbl);
add(lbl);
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
removeText++;
if (removeText == 1) {
lbl.setVisible(false);
lbl1.setBounds(100, 40, 100, 20);
lbl2.setBounds(100, 70, 100, 20);
lbl3.setBounds(100, 100, 150, 20);
lbl4.setBounds(100, 130, 100, 20);
}
if (removeText == 2) {
lbl1.setVisible(false);
lbl2.setBounds(100, 40, 100, 20);
lbl3.setBounds(100, 70, 150, 20);
lbl4.setBounds(100, 100, 100, 20);
}
if (removeText == 3) {
lbl2.setVisible(false);
lbl3.setBounds(100, 40, 150, 20);
lbl4.setBounds(100, 70, 100, 20);
}
if (removeText == 4) {
lbl3.setVisible(false);
lbl4.setBounds(100, 40, 100, 20);
}
if (removeText == 5) {
lbl4.setVisible(false);
}
}
}
Learning how to properly use layout managers will save you a lot of trouble in the long run.
You'll also find that people will tell you to adhere to the single responsibility principle, and avoid making classes that violate this principle (e.g., extending JFrame and implementing ActionListener).
You'll also hear folks tell you to prefer using actions over action listeners (if you need to share functionality across multiple components, that is).
A simple way would be to dedicate an entire panel to holding your labels, and simply remove the first label in the panel until there are no more labels. Here's an example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelDemo
{
public static void main(String[] args) {
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (String s: labels) {
panel.add(new JLabel(s));
}
frame.add(panel);
JButton button = new JButton("Select");
button.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
if (panel.getComponentCount() > 0)
panel.remove(0);
frame.repaint();
}
});
frame.add(button, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
Also, you may just have a certain goal in mind that I'm not aware of, but it honestly seems like a list would be better in this case. Here's an example of that as well:
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
JList<String> list = new JList<>(labels);
int option = JOptionPane.showConfirmDialog(null, list,
"Please tell us why you wish to return your purchase.",
JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
String selectedValue = list.getSelectedValue();
System.out.println(selectedValue); // Do something with it.
}
I want to create a jtext field with a definite size, but I would like it to expand like a scroll pane when the input grows. Here is what I have so far:
JButton b,b1;
JLabel j1,j2,j3;
JTextField t1,t2;
public Window(){
super("frame");
//new Thread(this).start();
b=new JButton("Click");
j1=new JLabel("VALUE1");
t1=new JTextField(30);
j2=new JLabel("VALUE2");j3=new JLabel(" ");
t2=new JTextField(30);
j1.setBounds(100, 50, 150, 50);
t1.setBounds(150, 50, 200, 50);
j2.setBounds(200, 150, 250, 150);
t2.setBounds(250, 150, 200, 50);j3.setBounds(300, 150, 250, 150);
b.setBorder(new LineBorder(Color.black));
b.setBackground(Color.black);
b.setForeground(Color.white);
b1=new JButton("Exit");
super.setSize(300,300);
super.setLocation(250,150);
super.setLayout(new FlowLayout());
super.setVisible(true);
add(j1);add(t1);add(j2);add(t2); add(j3);
add(b);add(b1);
b.addActionListener(this);
b1.addActionListener(this);
If you dont want to use JScrollPane this is something you are looking for -
I have created a sample program to auto resize the JTextField size
i.e if text is added its size increases and if text is deleted its size is reduced.
I have used KeyListener's - keyTyped event to detect and update the text field size.
You can add document listener too to do the same thing.
Here is my sample code -
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class AutoResizeTest
{
public AutoResizeTest(){
JFrame frame = new JFrame("Auto-Resizable TextField");
frame.setLayout(null);
MyCustomTextField expandableText = new MyCustomTextField(20);
expandableText.setBounds(10, 10, 200, 30);
expandableText.setPreferredSize(new Dimension(200,30));
expandableText.setMaximumSize(new Dimension(600,30));
frame.add(expandableText);
frame.setBounds(100,100,700,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
AutoResizeTest test = new AutoResizeTest();
}
class MyCustomTextField extends javax.swing.JTextField{
private int originallimit;
int previousLength;
int length;
public MyCustomTextField(int limit){
previousLength=0;
originallimit = limit;
this.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
JTextField textField = (JTextField) e.getSource();
length = textField.getText().length();
if(length >= originallimit){
if(length > previousLength){
textField.setSize(new Dimension(textField.getWidth()+10, textField.getHeight()));
}
else{
if(length < previousLength)
textField.setSize(new Dimension(textField.getWidth()-10, textField.getHeight()));
}
previousLength = length;
}else{
textField.setSize(textField.getPreferredSize());
}
}
});
}
}
}
In this code I have increamented/reduced the size by 10. You can put your desired value.
Hope that helps you.
Place the TextArea in a seperate JPanel with BorderLayout and in the center and the panel automatically expands, when the content increases.
textArea = new JTextArea(2, WIDTH);
textArea.setLineWrap(true);
textArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);
The default size of the JTextField will be set and provide a ScrollPane to JTextField which will make field to expand if necessary.
Try the following
JTextField jtext = new JTextField(pane);
JScrollPane scroll = new JScrollPane(jtext);
Someone told me a way to paint onto a Jframe and it worked fine in the example, but now I have tabs it doesn't paint onto them.
I need the paint/drawLine to work in my Tabbed example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
public class GUI extends JTabbedPane implements ActionListener
{
static JFrame aWindow = new JFrame("Project");
JTabbedPane myTabs = new JTabbedPane();
JPanel loginMainPanel = new JPanel();
JPanel displayMainPanel = new JPanel();
JPanel editMainPanel = new JPanel();
JTextField myText1 = new JTextField("");
JTextField myText2 = new JTextField("");
JTextField myText3 = new JTextField("");
JLabel loginLabel = new JLabel("Username:");
JTextField loginField = new JTextField();
JLabel loginLabel2 = new JLabel("Password:");
JPasswordField loginPass = new JPasswordField();
JButton displayButton = new JButton("Load Data");
JButton loginButton = new JButton("Login");
JLabel editLabel = new JLabel("Write:");
JTextArea editArea = new JTextArea();
public GUI()
{
Toolkit theKit = aWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();
aWindow.setBounds(wndSize.width/3, wndSize.height/3, 250, 250);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(1,1);
Container content = aWindow.getContentPane();
content.setLayout(grid);
createLoginPanel();
createDisplayPanel();
createEditPanel();
myTabs.addTab("Login", loginMainPanel);
myTabs.addTab("Main Menu", displayMainPanel);
myTabs.addTab("Setting", editMainPanel);
myTabs.setSelectedIndex(0);
myTabs.setEnabledAt(1,false);
myTabs.setEnabledAt(2,false);
content.add(myTabs);
aWindow.setVisible(true);
}
public void createLoginPanel()
{
loginMainPanel.setLayout(null);
loginLabel.setBounds(10, 15, 150, 20);
loginMainPanel.add(loginLabel);
loginField.setBounds(10, 35, 150, 20);
loginMainPanel.add(loginField);
loginLabel2.setBounds(10, 60, 150, 20);
loginMainPanel.add(loginLabel2);
loginPass.setBounds(10, 80, 150, 20);
loginMainPanel.add(loginPass);
loginButton.addActionListener(this);
loginButton.setBounds(50, 110, 80, 20);
loginMainPanel.add(loginButton);
}
public void createDisplayPanel()
{
displayMainPanel.setLayout(null);
displayButton.addActionListener(this);
displayButton.setBounds(50, 80, 150, 20);
displayMainPanel.add(displayButton);
myText1.setBounds(50, 170, 200, 30);
myText2.setBounds(50, 140, 200, 30);
myText3.setBounds(50, 110, 200, 30);
displayMainPanel.add(myText1);
displayMainPanel.add(myText2);
displayMainPanel.add(myText3);
}
public void createEditPanel()
{
editMainPanel.setLayout(null);
editLabel.setBounds(10, 15, 150, 20);
editMainPanel.add(editLabel);
editArea.setBounds(10, 65, 150, 50);
editMainPanel.add(editArea);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == loginButton)
{
//myTabs.setSelectedIndex(1);
}
}
public void paint(Graphics g) {
super.paint(g);
int locX = 0;
int locY = 0;
int destX = 210;
int destY = 210;
g.setColor(Color.red);
// draw a line (there is now drawPoint..)
g.drawLine(locX, locY, destX, destY);
}
public static void main(String[] args)
{
GUI tw1 = new GUI();
}
}
How can I find a solution so it will paint that line on the tab (loginMainPanel)?
If you want custom drawing on a JPanel, you should create a custom class that extends JPanel:
class CustomPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(x1, y1, x2, y2);
}
}
Then:
JPanel loginMainPanel = new JPanel();
Woudl become:
JPanel loginMainPanel = new CustomPanel();
Sorry for the blurge of bloated code
Yes, well that is why people can't solve problems, because the code is so bloated you can't see what you are doing.
If you want us to help you problem solve then you need to post a SSCCE
Someone told me a way to paint onto a Jframe
Well, that is wrong, in general you should not be overrding the paint() method, unless you have a specific reason.
Also, your whole program is wrong because you are extending JTabbedPane. You should never do this to create a GUI.
Your paint() method is never invoked because you never use that class anywhere. Look at your code. For your class variables you create a new JTabbedPane. Then in the constructor you add all these components to the frame and make the frame visible.
You need to take a look at the Swing tutorial and follow some of the example there for a better way to create a simple GUI.
I don't understand what you are trying to do by drawing a line on the tabbed pane. A tabbed pane displays different panels every time you click on a tab. Where exactly do you want the line to appear?
Also, you should learn how to use layout managers. Using a null layout will cause unnecessary problems.
Until you post a SSCCE, I can't be of much help.