Applet appearance change when it's converted to an executable - java

I hava a problem with the applet when it is converted into an executable. the applet has three buttons with text as shown in pic_1 below, and when it is converted into an executable, the text of the three buttons disappears and the buttons becomes smalle as shown in pic_2 below.
I downloaded some applications that generates the executable file from a .jar files, and the result after using these applications is the same, the applet runs but with no buttons.
please provide any suggestions to solve this issue.
code_init() method_:
public void init () {
frame = new JFrame ();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
/* Erzeuge MenuBar*/
menubar = new JMenuBar();
frame.setJMenuBar(menubar);
einstellungen = new JMenu ("Einstellungen");
datei = new JMenuItem ("Datei");
datei.addActionListener(this);
communication = new JMenuItem ("Kommunikation");
communication.addActionListener(this);
simulation = new JMenuItem ("Simulation");
simulation.addActionListener(this);
menubar.add(einstellungen);
einstellungen.add(datei);
einstellungen.add(communication);
einstellungen.add(simulation);
/*Erzeuge Layout */
panel1 = new JPanel ();
top = new JPanel ();
center = new JPanel ();
down = new JPanel ();
bottom = new JPanel ();
frame.add(panel1); //general Panel
panel1.setLayout(new BorderLayout ()); //Separate areas in BorderLayout
panel1.add(top, BorderLayout.NORTH);
panel1.add(center, BorderLayout.CENTER);
panel1.add(bottom, BorderLayout.SOUTH);
Lspeed = new JLabel ("Geschwindigkeit: ");
Lspeedanzeige = new JLabel (" ");
LineBorder lBorder = new LineBorder(new Color(100, 100, 100));
Lspeedanzeige.setBorder(lBorder);
JLabel Lfueller = new JLabel (" ");
Ldrive = new JLabel ("Motordrehzahl: ");
Ldriveanzeige = new JLabel (" ");
LineBorder rBorder = new LineBorder(new Color(100, 100, 100));
Ldriveanzeige.setBorder(rBorder);
top.setLayout(new FlowLayout ()); //add FlowLayout to Panel top
top.add(Lspeed);
top.add(Lspeedanzeige);
top.add(Lfueller);
top.add(Ldrive);
top.add(Ldriveanzeige);
simulationarea = new JTextArea ("Gesendete Daten:");
JScrollPane scrollPane = new JScrollPane(simulationarea);
simulationarea.setPreferredSize(new Dimension(5, 100));
simulationarea.setLineWrap(true);
simulationarea.setWrapStyleWord(true);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(5, 100));
scrollPane.setVisible(true);
simulationarea.setEditable(true);
simulationarea.setEnabled(true);
int pos = simulationarea.getCaretPosition();
simulationarea.insert("file logs", pos+17);
center.setLayout(new BorderLayout ());
center.add(simulationarea, BorderLayout.NORTH); //add JTextfield simulationarea to Panel center
center.add(down, BorderLayout.SOUTH); //add Panel down zu Panel Center
Bplay = new JButton ("Play");
Bplay.setPreferredSize(new Dimension(60, 50));
Bplay.addActionListener(this);
Bbreak = new JButton ("Pause");
Bbreak.addActionListener(this);
Bstop = new JButton ("Stop");
Bstop.addActionListener(this);
down.setLayout(new FlowLayout ()); //add FlowLayout to Panel down
down.add(Bplay);
down.add(Bbreak);
down.add(Bstop);
statusarea = new JTextArea ("Nicht verbunden");
statusarea.setForeground(Color.RED);
statusarea.setPreferredSize(new Dimension(550, 50));
bottom.add(statusarea);
frame.setVisible(true);
System.out.println("init");
}//init
Pic_1
pic_2

Related

how to make the text field next to the label in GUI

import java.awt.*;
import javax.swing.*;
public class guiAs {
public static void main (String args [] )
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension (300,300));
frame.setTitle("Calculator");
JPanel panel = new JPanel();
// JLabel label = new JLabel();
//creating num1
JPanel panel2 = new JPanel();
JLabel lable1 = new JLabel("Number 1 ");
JTextField tf1 = new JTextField();
//creating num2
JLabel lable2 = new JLabel("Number 2 ");
JTextField tf2 = new JTextField();
//creating result
JPanel panel3 = new JPanel();
JLabel lable3 = new JLabel("Result: ");
JTextField tf3 = new JTextField(10);
//creating button
JButton Add= new JButton("Add");
JButton Subtract = new JButton("Subtract");
JButton Multiply = new JButton("Multiply");
JButton Division = new JButton("Division");
//creating num1
panel2.add(lable1);
panel2.add(tf1);
//creating num2
panel2.add(lable2);
panel2.add(tf2);
//creating result
panel3.add(lable3);
panel3.add(tf3);
//creating buttons
panel.add(Add);
panel.add(Subtract);
panel.add(Multiply);
panel.add(Division);
// frame.getContentPane().add(BorderLayout.WEST, panel3);
//creating Box Layout for num1 and num2
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
frame.add(panel2);
//creating Box Layout for buttons
BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout1);
frame.setLayout(new FlowLayout());
frame.add(panel);
// Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
// panel.add(Add);
// frame.getContentPane().add(BorderLayout.SOUTH, panel);
//creating Border Layout for num1 and num2
frame.getContentPane().add(BorderLayout.WEST, panel2);
//creating Border Layout for Buttons
frame.getContentPane().add(BorderLayout.EAST, panel);
//creating Box Layout for Result
frame.getContentPane().add(BorderLayout.SOUTH, panel3);
frame.setVisible(true);
}
}
this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves
its work only with the result but for the number doesn't work
here i used Box Layout for buttons and number 1 and number 2:
You were very close to solving it.
Replace this line
JPanel panel2 = new JPanel();
With this line
JPanel panel2 = new JPanel(new GridLayout(2, 2));
Then, get rid of these 3 lines
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
Now your code looks like this

Java GUI Fill Empty spaces

I have the following Java GUI for my project. For some reason, the middle and right panels have been put below leaving empty spaces - marked in red. Is there a way to pull the panels up to fill the spaces.
These empty spaces does not look good. I have tried to check all code but cannot pinpoint where to correct.
Thanks in advance.
public static JPanel main= new JPanel(),
left= new JPanel(),middle= new JPanel(),right = new JPanel();
Container c = getContentPane();
c.setLayout(new FlowLayout()) ;
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(new JLabel("Enter word/SQL: "), "North");
pane.add(tf, "Center"); //enter keyword
JPanel second = new JPanel();
second.setLayout(new GridLayout(8, 1));
second.add(messageIDText);
//This is the start of adding panels
//create a left side panal and add the sub panels to it
JPanel left = new JPanel();
JPanel middle = new JPanel();
JPanel center = new JPanel();
JTextArea output = new JTextArea("", 60, 60);
JPanel sixth = new JPanel();
JPanel seventh = new JPanel();
JPanel eigth = new JPanel(new GridLayout(8,1));
JPanel labels7 = new JPanel(new GridLayout(5,1));
JPanel controls7 = new JPanel(new GridLayout(5,1));
JPanel tenth = new JPanel();
sixth.setLayout(new BorderLayout(5,5));
JPanel fourth = new JPanel();
fourth.add(firstButton);
sixth.add(labels, BorderLayout.WEST);
sixth.add(controls, BorderLayout.CENTER);
sixth.add(controlsButtons, BorderLayout.EAST);
labels.add(new JLabel("Proposal:"));
controls.add(proposalNumberText);
controlsButtons.add(freeQueryButton);
//add border
pane.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
fourth.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
//-------LEFT SIDE -- PARAMETERS
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
JLabel headerLabel,blank;
headerLabel = new JLabel("Navigate Records", JLabel.LEFT);
Font f = headerLabel.getFont();
headerLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD)); // bold
left.add(headerLabel, BorderLayout.PAGE_END);
left.add(fourth, BorderLayout.PAGE_END);
main.add(left);
//---------MIDDLE PART - Messages - tabbed pane
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
//jtp.setSize(jtp.getPreferredSize());
JPanel jpA = new JPanel(),jpB = new JPanel(),
jpC = new JPanel(),jpD = new JPanel();
JScrollPane scrollPane2 = new JScrollPane(),jp = new
JScrollPane(activeTSText), jp2 = new JScrollPane(analyseWordsText),
jp3 = new JScrollPane(markedMessageText);
JLabel labelA = new JLabel();
labelA.setText("");
jpA.add(labelA);
jpA.add(jp);
jtp.addTab("Message", jpA);
middle.add(jtp);
main.add(middle);
//----------RIGHT PART - .. //paramters and results
JPanel jj = new JPanel();
jj.setLayout(new BoxLayout(jj, BoxLayout.PAGE_AXIS));
stateJTable.setRowHeight(20); //add JTable
JScrollPane js3 =new JScrollPane(stateJTable);
js3.setVisible(true);
//JTabbedPane jtp2 = new JTabbedPane();
//JPanel StateSubstatesTabOnLeft = new JPanel(),
StateSubstates_PostProcessedTabOnLeft = new JPanel();
JPanel firstleftTabinRightSideOfFrame = new JPanel(),
secondLeftTabinRightSideOfFrame = new
JPanel(),rightTabInRightSideOfFrame = new JPanel();
jj.add(js3);
//downbelow, the state jtable is added to the panel here through the
//scrollpane which contains the jtable
right.add(jj) ; //stateJTable);// add table in panel using add() method
main.add(right);
proposalNumberText.requestFocusInWindow();
I have solved the problem by using Borderlayout and assigning the main panel to it.
The assigning the WEST, CENTER and EAST regions to it.
I have added the code below in the code above at places where the main panel is being added to.
Thank you for the advice to start debugging by using MRE approach.
BorderLayout layout = new BorderLayout();
main.setLayout(layout);
main.add(left,BorderLayout.WEST);
main.add(middle,BorderLayout.CENTER);
main.add(right,BorderLayout.EAST);

Packing three JPanels within a JFrame

I'm trying to fit three JPanel (dataPanel, tablePanel and btnPanel), so this is how far I got:
The thing is that the middle panel (tablePanel) is cropped on the top and the header is not visible. Also I added a JScrollPane to it that doesn't appear either. I'm new to Java Swing, so here's my code:
JFrame frame = new JFrame("Crear pedido");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
//frame.setSize(300, 150);
// or frame.pack() to "pack" all the components in this frame
frame.setVisible(true); // show it
frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.X_AXIS));
//frame.add((Component) BorderFactory.createEmptyBorder(30,0,0,0));
dataPanel.setBorder(new EmptyBorder(20,0,30,0));
frame.add(dataPanel, BorderLayout.NORTH);
JLabel id = new JLabel("ID:");
JLabel date = new JLabel("Fecha:");
JLabel dept = new JLabel("Departamento");
JTextField idInput = new JTextField (5);
idInput.setMaximumSize(new Dimension(70,25));
JFormattedTextField dateInput = new JFormattedTextField(df);
dateInput.setMaximumSize(new Dimension(70,25));
JComboBox deptSelect = new JComboBox(selectArray);
deptSelect.setMaximumSize(new Dimension(70,25));
id.setAlignmentX(Component.CENTER_ALIGNMENT);
date.setAlignmentX(Component.CENTER_ALIGNMENT);
dept.setAlignmentX(Component.CENTER_ALIGNMENT);
idInput.setAlignmentX(Component.CENTER_ALIGNMENT);
dateInput.setAlignmentX(Component.CENTER_ALIGNMENT);
deptSelect.setAlignmentX(Component.CENTER_ALIGNMENT);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(id);
dataPanel.add(idInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(date);
dataPanel.add(dateInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(dept);
dataPanel.add(deptSelect);
JPanel productPanel = new JPanel();
productPanel.setLayout(new BoxLayout(productPanel, BoxLayout.Y_AXIS));
frame.add(productPanel, BorderLayout.CENTER);
JTable table = new JTable(data, headerProductos);
table.setMaximumSize(new Dimension(500,250));
table.setAlignmentX(Component.CENTER_ALIGNMENT);
table.setAlignmentY(Component.CENTER_ALIGNMENT);
table.getTableHeader().setForeground(Color.blue);
table.setEnabled(false);
JScrollPane scroll = new JScrollPane(table);
productPanel.add(table);
table.add(scroll);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
btnPanel.setBorder(new EmptyBorder(150,0,0,0));
frame.add(btnPanel, BorderLayout.SOUTH);
Replace lines:
productPanel.add(table);
table.add(scroll);
with:
productPanel.add(scroll);

Positioning/displaying JLayeredPane

I have Panel that contains JLayeredPane which contains two JPanels and each of them two JLabels.
Panel panel2 = new Panel();
JLayeredPane westP = new JLayeredPane();
westP.setLayout(null);
westP.setPreferredSize(new Dimension(100,400));
westP.setBackground(Color.blue);
JPanel layerOne = new JPanel();
jl1 = new JLabel("player1: ");
layerOne.add(jl1);
layerOne.setBounds(5, 100, 50, 50);
jl1Add = new JLabel("");
layerOne.add(jl1Add);
westP.add(layerOne,0, 0);
similar for another JPanel except
westP.add(LayerTwo,0,1);
and later:
panel2.setLayout(new BorderLayout());
panel2.add(westP);
add(panel2, BorderLayout.WEST);
later when I simply test it, I get lbl1, lbl1Add, lbl2 shown on GUI but not lbl2Add?
if (activeColour==RED)
{
timesPlayedRed++;
jl1Add.setText("");
jl1Add.setText(String.valueOf((timesPlayedRed)));
activeColour=YELLOW;
}
else
{
timesPlayedYellow++;
jl2Add.setText("");
jl2Add.setText(String.valueOf((timesPlayedYellow)));
activeColour=RED;
}
I am just trying out java and netbeans.

Why is my Menu on the bar displaying below a JPanel in the container?

I'm in the position where I have to reuse and modify another persons code to create an image processing application. The following code is used to setup the frame and its GUI. The problem I'm having is that when the file word is clicked on the menubar the menuitems are listed below the canvas contained in the originalImage container.
Here is the code:
public ImageLoader(){
super("IRIS Application");
JMenu fileMenu = new JMenu("File");
JMenuItem fileOpenChoice = new JMenuItem("Open File");
fileOpenChoice.addActionListener(new FileOpener());
fileMenu.add(fileOpenChoice);
JMenuItem fileSaveChoice = new JMenuItem("Save File");
fileSaveChoice.addActionListener(new FileSaver());
fileMenu.add(fileSaveChoice);
JMenu processMenu = new JMenu("Process");
JMenuItem reduceContrast = new JMenuItem("Reduce Contrast");
reduceContrast.addActionListener(new ReduceContrast());
processMenu.add(reduceContrast);
JMenuItem scale = new JMenuItem("Scale");
scale.addActionListener(new Scaler());
processMenu.add(scale);
JMenuItem sobel = new JMenuItem("Sobel");
sobel.addActionListener(new Sobel());
processMenu.add(sobel);
JMenuItem prewitt = new JMenuItem("Prewitt");
prewitt.addActionListener(new Prewitt());
processMenu.add(prewitt);
originalImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
processedImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
originalRaster = originalImage.getRaster();
processedRaster = processedImage.getRaster();
ic = new ImageCanvas();
pc = new ImageCanvas();
Container content = this.getContentPane();
content.setLayout(new FlowLayout());
JPanel originalContainer = new JPanel();
originalContainer.add(ic);
originalContainer.setSize(ic.getWidth(), ic.getHeight());
originalContainer.setBorder(new EtchedBorder());
JPanel processedContainer = new JPanel();
processedContainer.add(pc);
processedContainer.setSize(pc.getWidth(), pc.getHeight());
processedContainer.setBorder(new EtchedBorder());
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new FlowLayout());
imagePanel.add(originalContainer);
imagePanel.add(processedContainer);
//content.add(imagePanel, BorderLayout.NORTH);
JPanel originalGrid = new JPanel();
originalGrid.setLayout(new GridLayout(0,1,5,0));
JPanel processedGrid = new JPanel();
processedGrid.setLayout(new GridLayout(0,1,5,0));
originalText = new JTextField("Original Text");
JButton originalButton = new JButton("View Coordinates Table");
originalGrid.add(originalText);
originalGrid.add(originalButton);
JTextField processedText = new JTextField("Processed Text");
JButton processedButton = new JButton("View Coordinates Table");
processedGrid.add(processedText);
processedGrid.add(processedButton);
JPanel informationPanel = new JPanel();
informationPanel.setBackground(Color.red);
informationPanel.setLayout(new BorderLayout());
informationPanel.add(originalGrid, BorderLayout.WEST);
informationPanel.add(processedGrid, BorderLayout.EAST);
//content.add(informationPanel, BorderLayout.SOUTH);
ic.addMouseMotionListener(new ImageMouseMotionListener(ic, originalText));
pc.addMouseMotionListener(new ImageMouseMotionListener(pc, processedText));
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
bar.add(processMenu);
content.add(imagePanel, BorderLayout.NORTH);
content.add(informationPanel, BorderLayout.SOUTH);
setJMenuBar(bar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 725);
setResizable(false);
setVisible(true);
setLocation(125, 0);
}
I know the menu bar is fine so I'm assuming its the way I've added the canvas to the JPanel. Should a set its z order in the JPanel?
I've read through the books I have at home and nothing mentions this kind of problem.
Thanks,
Daniel
Mixing AWT and Swing component will cause overlap issue, Canvas is a AWT component, use JPanel instead
After JDK1.6_12, you can use both safety.

Categories