我正在构建一个独立的应用程序,它将文本隐藏在图像上(Stenography)
我是新手,在实现GridBagLayout
的功能时遇到错误这里,我使用以下算法的错误 (我已经提到了评论中的所有错误,即//错误:.........)
import java.awt.Color;
import java.awt.Insets;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
public class Steganography_View extends JFrame
{
//sie variables for window
private static int WIDTH = 500;
private static int HEIGHT = 400;
//elements for JPanel
private JTextArea input;
private JScrollBar scroll,scroll2;
private JButton encodeButton,decodeButton;
private JLabel image_input;
//elements for Menu
private JMenu file;
private JMenuItem encode;
private JMenuItem decode;
private JMenuItem exit;
public Steganography_View(String name)
{
//set the title of the JFrame
super(name);
//Menubar
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File"); file.setMnemonic('F');
encode = new JMenuItem("Encode"); encode.setMnemonic('E'); file.add(encode);
decode = new JMenuItem("Decode"); decode.setMnemonic('D'); file.add(decode);
file.addSeparator();
exit = new JMenuItem("Exit"); exit.setMnemonic('x'); file.add(exit);
menu.add(file);
setJMenuBar(menu);
// display rules
setResizable(true); //allow window to be resized: true?false
setBackground(Color.lightGray); //background color of window: Color(int,int,int) or Color.name
setLocation(100,100); //location on the screen to display window
setDefaultCloseOperation(EXIT_ON_CLOSE);//what to do on close operation: exit, do_nothing, etc
setSize(WIDTH,HEIGHT); //set the size of the window
setVisible(true); //show the window: true?false
}
public JMenuItem getEncode() { return encode; }
public JMenuItem getDecode() { return decode; }
public JMenuItem getExit() { return exit; }
public JTextArea getText() { return input; }
public JLabel getImageInput() { return image_input; }
public JPanel getTextPanel() { return new Text_Panel(); }
public JPanel getImagePanel() { return new Image_Panel(); }
public JButton getEButton() { return encodeButton; }
public JButton getDButton() { return decodeButton; }
private class Text_Panel extends JPanel
{
public Text_Panel()
{
//setup GridBagLayout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
setLayout(layout);
input = new JTextArea();
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,0,0,0);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 50.0;
JScrollPane scroll = new JScrollPane(input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
//Error;The constructor JScrollPane(JTextArea, int, int) is undefined
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//Error;The constructor JScrollPane(JTextArea, int, int) is undefined
layout.setConstraints(scroll,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));// Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
add(scroll);//Error;The method add(Component) in the type Container is not applicable for the arguments (JScrollPane)
encodeButton = new JButton("Encode Now");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,-5,-5,-5);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(encodeButton,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)
add(encodeButton);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)
//set basic display
setBackground(Color.lightGray);
setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method setBorder(Border) is undefined for the type Steganography_View.Text_Panel
}
}
private class Image_Panel extends JPanel
{
public Image_Panel()
{
//setup GridBagLayout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
setLayout(layout);
image_input = new JLabel();
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,0,0,0);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 50.0;
JScrollPane scroll2 = new JScrollPane(image_input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,//Error;The constructor JScrollPane(JLabel, int, int) is undefined
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//Error;The constructor JScrollPane(JLabel, int, int) is undefined
layout.setConstraints(scroll2,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
scroll2.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method setBorder(Border) is undefined for the type JScrollPane
image_input.setHorizontalAlignment(JLabel.CENTER);
add(scroll2);//Error;The method setBorder(Border) is undefined for the type JScrollPane
decodeButton = new JButton("Decode Now");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,-5,-5,-5);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(decodeButton,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)
add(decodeButton);//Error;The method add(Component) in the type Container is not applicable for the arguments (JButton)
//set basic display
setBackground(Color.lightGray);
setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method add(Component) in the type Container is not applicable for the arguments (JButton)
}
}
public static void main(String args[])
{
new Steganography_View("Secure Data Transmission");
}
}
请帮我调试一下。
答案 0 :(得分:2)
使用有利于您的Java文档,这样您就可以清楚地了解其库方法的工作原理。 The complete documentation can be found here. 强>
你的斗争可以被视为纯粹的懒惰或缺乏关于Java Documentation(JavaDocs)的知识。
请仔细查看您的错误,因为他们会明确指导您找到解决方案。
<强>示例:强>
构造函数JScrollPane(JTextArea,int,int)未定义。
JScrollPane有four constructors。
public JScrollPane()
public JScrollPane(Component view)
public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
public JScrollPane(int vsbPolicy, int hsbPolicy)
你需要使用提供的一个来构建你的对象,否则它将不起作用......
Container类型中的方法add(Component)不适用于参数(JButton)
请查看Component documentation,了解如何使用add()
方法......
GridBagLayout类型中的方法setConstraints(Component,GridBagConstraints)不适用于参数(JScrollPane,GridBagConstraints)
您提供的参数与setConstraints
GridBagLayout
方法的预期参数不同,因为您传递的参数不同于clearly explained here的参数:
public void setConstraints(Component comp, GridBagConstraints constraints)
comp
- 要修改的组件constraints
- 要应用的约束......等等。