我正在製作一個窗口,其中有一個大的文本區域和一個小的文本區域。這是我到目前爲止有:JLabel和JTextArea不起作用
這是我有它的代碼:
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon ii=new ImageIcon("textEffect.png");
Image i=ii.getImage();
g.drawImage(i,0,0,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//main method
public static void main(String[] args){
new Window();
}
//Makes window and starts bot
public Window(){
super("Pollockoraptor");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setOpaque(false);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setLineWrap(true);
input.addKeyListener(this);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.LEFT);
window.add(scroll);
window.add(label);
window.add(input);
window.setBackground(new Color(97,118,131));
add(window);
setVisible(true);
}'
如何讓我大textarea的部分透明的,所以我可以看到背景,以及如何我是否將「說:」作爲小文字區的前面?
我真的不禁首先沒有看到你的[mcve](http://stackoverflow.com/help/mcve),但有一些不相關的建議:1)不要像你那樣用paintComponent方法讀取圖像不必要地重新閱讀圖像並放慢必須絕對快速的方法。另外,2)永遠不要將KeyListener添加到文本組件中,我不關心Swing教程展示了這一點,這只是一個危險的事情,並且有更好的方法來捕獲文本組件中的輸入。3)您的佈局經理應該幫助您爲您定位組件。 – 2014-10-03 02:14:43
哦,4)遵循MadProgrammer的建議,因爲他知道Swing向前和向後。 – 2014-10-03 02:16:55
'JTextArea input = new JTextArea(1,46);'應該可能是'JTextField input = new JTextField(46);' – 2014-10-03 02:28:45