Question

Can you help me to link two frames in Java Swing and then if you click...

Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :)

First frame code:

//First

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Sample extends JFrame implements ActionListener {
JMenuBar mb;
JMenu file;
JMenuItem open;
JTextArea ta;
JButton proceed= new JButton("Proceed");
Sample() {
open = new JMenuItem("Open File");
open.addActionListener(this);
file = new JMenu("File");
file.add(open);
mb = new JMenuBar();
mb.setBounds(0, 0, 400, 20);
mb.add(file);
ta = new JTextArea(400, 400);
ta.setBounds(0, 20, 400, 400);
proceed.setBounds(200, 450, 100, 40);
proceed.addActionListener(new ActionListener() {
  
@Override
public void actionPerformed(ActionEvent a) {
secondbox s = new secondbox();
}
});
add(proceed);
add(mb);
add(ta);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
String filepath = f.getPath();
try {
BufferedReader br = new BufferedReader(new FileReader(filepath));
String s1 = "", s2 = "";
while ((s1 = br.readLine()) != null) {
s2 += s1 + "\n";
}
ta.setText(s2);
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}

}
}
}

public static void main(String[] args) {
First om = new First();
om.setSize(600,600);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Second Frame Code:

import javax.swing.*;
import java.awt.event.*;
class Second implements ActionListener{
JLabel l1,l2;
JTextArea area;
JButton b,b1,b2;
JFrame f;

Second() {
f= new JFrame();
l1=new JLabel();
l1.setBounds(120,25,120,50);
l2=new JLabel();
l2.setBounds(290,25,100,50);
area=new JTextArea();
area.setBounds(120,90,280,200);
b=new JButton("Count Char");
b.setBounds(280,300,120,30);
b1=new JButton("Clear"); //added feature
b1.setBounds(120,300,120,30);
b.addActionListener(this);
b.addActionListener((e -> {
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
int count=0;
for (int i=0;i<text.length();i++){
if(text.charAt(i)!=' ')
count++;
}
l2.setText("Char: "+count);
b.setVisible(false);
b1.setVisible(false);
b2=new JButton("Proceed");
b2.setBounds(280,300,120,30);
f.add(b2);
}));
b1.addActionListener((e -> {
area.setText("");
}));
f.add(l1);f.add(l2);f.add(area);f.add(b);f.add(b1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
  
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("No.of Words: "+words.length);
int count=0;
for (int i=0;i<text.length();i++){
if(text.charAt(i)!=' ')
count++;
}
l2.setText("No.of Char: "+count);
}
public static void main(String[] args) {
new Second();   
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

if you have any Query comment Down below

SIMPLY copy the code in two classes and run

SS:

- O X - 0 X Clear Clear C ount Char Proceed

Code:

Class#1

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Sample extends JFrame implements ActionListener {
JMenuBar mb;
JMenu file;
JMenuItem open;
JTextArea ta;
JButton proceed= new JButton("Proceed");
Sample() {
open = new JMenuItem("Open File");
open.addActionListener(this);
file = new JMenu("File");
file.add(open);
mb = new JMenuBar();
mb.setBounds(0, 0, 400, 20);
mb.add(file);
ta = new JTextArea(400, 400);
ta.setBounds(0, 20, 400, 400);
proceed.setBounds(200, 450, 100, 40);
proceed.addActionListener(new ActionListener() {
  
@Override
public void actionPerformed(ActionEvent a) {
Second s = new Second();
}
});
add(proceed);
add(mb);
add(ta);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
String filepath = f.getPath();
try {
BufferedReader br = new BufferedReader(new FileReader(filepath));
String s1 = "", s2 = "";
while ((s1 = br.readLine()) != null) {
s2 += s1 + "\n";
}
ta.setText(s2);
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}

}
}
}

public static void main(String[] args) {
Sample om = new Sample();
om.setSize(600,600);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Class #2..


import javax.swing.*;
import java.awt.event.*;
public class Second implements ActionListener{
JLabel l1,l2;
JTextArea area;
JButton b,b1,b2;
JFrame f;

Second() {
f= new JFrame();
l1=new JLabel();
l1.setBounds(120,25,120,50);
l2=new JLabel();
l2.setBounds(290,25,100,50);
area=new JTextArea();
area.setBounds(120,90,280,200);
b=new JButton("Count Char");
b.setBounds(280,300,120,30);
b1=new JButton("Clear"); //added feature
b1.setBounds(120,300,120,30);
b.addActionListener(this);
b.addActionListener((e -> {
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
int count=0;
for (int i=0;i<text.length();i++){
if(text.charAt(i)!=' ')
count++;
}
l2.setText("Char: "+count);
b.setVisible(false);
b1.setVisible(false);
b2=new JButton("Proceed");
b2.setBounds(280,300,120,30);
f.add(b2);
}));
b1.addActionListener((e -> {
area.setText("");
}));
f.add(l1);f.add(l2);f.add(area);f.add(b);f.add(b1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
  
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("No.of Words: "+words.length);
int count=0;
for (int i=0;i<text.length();i++){
if(text.charAt(i)!=' ')
count++;
}
l2.setText("No.of Char: "+count);
}
public static void main(String[] args) {
new Second();   
}
}

Add a comment
Know the answer?
Add Answer to:
Can you help me to link two frames in Java Swing and then if you click...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import...

    Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener {    public static final int width = 500;    public static final int height = 300;    public static final int no_of_lines = 10;    public static final int chars_per_line = 20;    private JTextArea lan1;    private JTextArea lan2;    public static void main(String[] args){        translatorApp gui = new translatorApp();...

  • How can i make the java class seperate into an MVC pattern? public class ChatView implements Acti...

    How can i make the java class seperate into an MVC pattern? public class ChatView implements ActionListener {    public static void main(String[] args)    {        JFrame chatFrame = new JFrame();        JFrame chatFrame1 = new JFrame();        JFrame chatFrame2 = new JFrame();               JPanel chatPanel = new JPanel();        JPanel chatPanel1 = new JPanel();        JPanel chatPanel2 = new JPanel();        chatFrame.setContentPane(chatPanel);        chatFrame1.setContentPane(chatPanel1);        chatFrame2.setContentPane(chatPanel2);        chatFrame.setLayout(new FlowLayout());        chatFrame1.setLayout(new FlowLayout());        chatFrame2.setLayout(new FlowLayout());        JTextField chatWrite = new JTextField();        JTextField chatWrite1 = new JTextField();        JTextField chatWrite2 = new JTextField();        JTextArea chatDisplay...

  • Can you fix my error? I created a program that changes labels every second in Java....

    Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. 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.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {...

    import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {    public static void createAndShowGUI() {        JFrame frame = new JFrame("Q1");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        Font courierFont = new Font("Courier", Font.BOLD, 40);        Font arialFont = new Font("Arial", Font.BOLD, 40);        Font sansFont = new Font("Sans-serif", Font.BOLD, 20);        JLabel label = new JLabel("Enter a word"); label.setFont(sansFont);        Font font1 = new Font("Sans-serif", Font.BOLD, 40);       ...

  • The program below is a GUI hander for a program with the following components. private int...

    The program below is a GUI hander for a program with the following components. private int count = 0; private JLabel label; private JButton button; The handler below is for the botton. Briefly summarize what happens when the user clicks the button. public class ButtonListerner implements ActionListener {public void actionPerformed (ActionEvent e) {count ++; if (count X2 == 0) {contentPame, setBackground(color. red); label. setText ("Go");}} if (count > 10} {button .SetEnabled (false); label, setText ("ARRIVED"); contentpane. setBackground (Color. gray);}}}

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT