Java Simple Window Program! You will write a program that creates a window and d
ID: 3574123 • Letter: J
Question
Java
Simple Window Program!
You will write a program that creates a window and displays the line you read in from the input file.
You can create the input file with Notepad
input.txt
Why’s that plane dusting crops where there ain’t no crops?
You will have to read the file name and open it in main, read in the line. Pass the line to routine that creates the window and then the one that creates the content.
Don’t forget the try block for the I/O to the file.
You may have to adjust the window size to handle the line from the file.
Compile and test your code.
A sample run:
java DisplayLine
Enter filename: input.txt
Look at the sample code below to help you:
//This program displays a label in a window
import javax.swing.*;
import java.awt.*;
public class SimpleWindow extends JFrame {
private static final int WIDTH = 250;
private static final int HEIGHT = 100;
/**
* Default Constructor
*/
public SimpleWindow() {
setTitle("Simple Window");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
} // end SimpleWindow constructor
//******************************************************
private void createContents()
{
JLabel label = new JLabel("Hi! I'm Louis the label!");
add(label);
} // end createContents
}
public class MainDriver {
public static void main(String[] args) {
new SimpleWindow();
System.out.println("Completed window display");
}
}
E Display File Contents Why's that plane dusting crops where there ain't no crops?Explanation / Answer
Here is the modified code:
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
@SuppressWarnings("serial")
public class SimpleWindow extends JFrame {
private static final int WIDTH = 500;
private static final int HEIGHT = 200;
/**
* Default Constructor
*/
public SimpleWindow() {
setTitle("Simple Window");
setSize(WIDTH, HEIGHT);
setLocation(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
read_File("test.txt"); // pass the exact location of your file which has to be read
setVisible(true);
} // end SimpleWindow constructor
//******************************************************
public String read_File(String filename)
{
String content = null;
File my_file = new File(filename);
FileReader file_reader = null;
try {
file_reader = new FileReader(my_file);
char[] chars = new char[(int) my_file.length()];
file_reader.read(chars);
content = new String(chars);
file_reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(file_reader!=null){try {
file_reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
createContents(content);
return content;
}
private void createContents(String content)
{
JLabel label = new JLabel(content);
add(label);
} // end createContents
}
Code for main class:
public class MainDriver {
public static void main(String[] args) {
new SimpleWindow();
System.out.println("Completed window display");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.