Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to make the program look like this? ( a ) ( b ) here is my code import javax.swing. * ; import java.awt. *

How to make the program look like this? (a)
(b) here is my code import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main extends JFrame {
private JTextField fileNameField;
private JTextArea binaryTextArea;
public Main(){
setTitle("Binary Editor");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
fileNameField = new JTextField();
binaryTextArea = new JTextArea();
binaryTextArea.setEditable(true);
JButton loadButton = new JButton("Load");
JButton saveButton = new JButton("Save");
loadButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
loadFile();
}
});
saveButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
saveFile();
}
});
JPanel topPanel = new JPanel(new FlowLayout());
topPanel.add(new JLabel("File Name:"));
topPanel.add(fileNameField);
topPanel.add(loadButton);
topPanel.add(saveButton);
add(topPanel, BorderLayout.NORTH);
add(new JScrollPane(binaryTextArea), BorderLayout.CENTER);
}
private void loadFile(){
String fileName = fileNameField.getText();
try {
Path filePath = Paths.get(fileName);
byte[] fileBytes = Files.readAllBytes(filePath);
String binaryRepresentation = bytesToBinary(fileBytes);
binaryTextArea.setText(binaryRepresentation);
} catch (IOException ex){
JOptionPane.showMessageDialog(this, "Error loading file: "+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void saveFile(){
String fileName = fileNameField.getText();
String binaryCode = binaryTextArea.getText();
try {
byte[] modifiedBytes = binaryToBytes(binaryCode);
Path filePath = Paths.get(fileName);
Files.write(filePath, modifiedBytes);
JOptionPane.showMessageDialog(this, "File saved successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex){
JOptionPane.showMessageDialog(this, "Error saving file: "+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private String bytesToBinary(byte[] bytes){
StringBuilder binaryStringBuilder = new StringBuilder();
for (byte b : bytes){
binaryStringBuilder.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace('','0'));
}
return binaryStringBuilder.toString();
}
private byte[] binaryToBytes(String binary){
int length = binary.length();
byte[] result = new byte[length /8];
for (int i =0; i length; i +=8){
String byteStr = binary.substring(i, i +8);
result[i /8]=(byte) Integer.parseInt(byteStr,2);
}
return result;
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new Main().setVisible(true);
}
});
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions