[英]How do I add JPanel/JSplitPane components side by side in a Frame?
I'm new to Java GUI and in need of some help. 我是Java GUI的新手,需要一些帮助。 I'm developing an application which loads an image from the HDD and displays it in the first window shown in the output below (desired output). 我正在开发一个应用程序,该应用程序将从HDD加载图像并将其显示在下面的输出(所需的输出)中显示的第一个窗口中。 I'm not quite sure about the layout to be used in this scenario. 我不太确定要在这种情况下使用的布局。 Tried GridBagLayout but I don't seem to get the exact output. 尝试了GridBagLayout,但是我似乎没有得到确切的输出。 I need a 2x3 layout where the first row with 3 columns consists of 3 labels with name IMAGE and the first cell of the second row consists of the image. 我需要2x3布局,其中具有3列的第一行包含3个名称为IMAGE的标签,第二行的第一单元格由图像组成。 My questions are: 我的问题是:
How do I make the image get displayed in the first cell of the second column alone 如何使图像单独显示在第二列的第一个单元格中
I have included a MenuItem called 'Transform' in the 'Option1' Menu. 我在“ Option1”菜单中包含了一个名为“ Transform”的MenuItem。 How do I write code such that when the 'Transform' MenuItem is clicked, the image in the first cell (2x1) gets cleared and gets displayed in the second cell (2x2). 如何编写代码,这样,当单击“转换”菜单项时,第一个单元格(2x1)中的图像将被清除并显示在第二个单元格(2x2)中。
I've used File class to deal with the Image load and save options. 我使用File类来处理图像加载和保存选项。 Is it okay? 可以吗 I had read that there's an 'Image' class in the AWT package to deal with image data but I'm not quite sure about it's implementation. 我已经读过AWT包中有一个“ Image”类来处理图像数据,但是我不确定它的实现。
I've included my code below... Please Help! 我在下面包含了我的代码...请帮助!
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class WorkingImage extends JFrame implements ActionListener
{
JMenuItem Open, Close, Save1, Save2, Save3, Transform1, Transform2;
JFileChooser choose;
JPanel panel;
Label l1, l2, l3;
/*public void myLayout()
{
panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
}*/
WorkingImage(String title)
{
super(title);
//myLayout();
JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
//File Menu, Open, Save, Close Menu Items
JMenu file = new JMenu("File");
mbar.add(file);
Open = new JMenuItem("Open");
Open.setMnemonic('O');
Open.addActionListener(this);
Close = new JMenuItem("Close");
Close.setMnemonic('E');
Close.addActionListener(this);
Save1 = new JMenuItem("Save");
Save1.setMnemonic('A');
Save1.addActionListener(this);
file.add(Open);
file.add(Save1);
file.add(Close);
//Creation of Option 1 and Option 2 Menus
JMenu opt1 = new JMenu("Option1");
mbar.add(opt1);
Transform1 = new JMenuItem("Transform");
Transform1.addActionListener(this);
Save2 = new JMenuItem("Save");
Save2.addActionListener(this);
opt1.add(Transform1);
opt1.add(Save2);
JMenu opt2 = new JMenu("Option2");
mbar.add(opt2);
Transform2 = new JMenuItem("Transform");
Transform2.addActionListener(this);
Save3 = new JMenuItem("Save");
Save3.addActionListener(this);
opt2.add(Transform2);
opt2.add(Save3);
//Set Frame Size
setSize(800, 600);
setVisible(true);
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
// Move the window
setLocation(x, y);
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e)
{
if(e.getSource() == Close)
{
System.out.println("\nApplication Terminated...");
System.exit(0);
}
else if(e.getSource() == Open)
{
choose = new JFileChooser();
choose.setDialogTitle("Specify a file to Open");
//Set File Extension filter
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("jpeg, jpg, png files", "jpeg", "jpg", "png");
choose.setFileFilter(filter);
int userSelection = choose.showOpenDialog(this);
if(userSelection == JFileChooser.APPROVE_OPTION)
{
File fileToOpen = choose.getSelectedFile();
}
}
else if(e.getSource() == Save1)
{
choose = new JFileChooser();
choose.setDialogTitle("Specify a file to save");
//Set file extension filter
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".jpeg, .jpg and .png files", "jpeg", "jpg", "png");
choose.setFileFilter(filter);
int userSelection = choose.showSaveDialog(this);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File fileToSave = choose.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
}
else
{
}
}
}
class JavaImage
{
public static void main(String args[])
{
new WorkingImage("Image Display");
}
}
Take a look at this implementation. 看一下这个实现。 It will make use of 2 Layout Managers. 它将使用2个布局管理器。 I used different components. 我使用了不同的组件。
public class SOAnswer extends JFrame{
private void initComponents(){
JPanel topPanel = new JPanel(new GridLayout(1, 3, 10, 10));// 1 row, 3 columns
topPanel.add(new JButton("IMAGE"));
topPanel.add(new JButton("IMAGE"));
topPanel.add(new JButton("IMAGE"));
JPanel bottomPanel = new JPanel(new GridLayout(1, 3, 10, 10));
bottomPanel.add(new JButton("CLICK ME"));
bottomPanel.add(new JButton("CLICK ME"));
bottomPanel.add(new JButton("CLICK ME"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setLayout(new BorderLayout(10, 10));
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[]args){
SOAnswer go = new SOAnswer();
go.initComponents();
}
}
Here's the output 这是输出
It's all about experimentation of the Layouts. 这都是关于布局的实验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.