Hi!
I am a beginner and I am learning to program on my spare time. I am working with a website to have a goal with learning Java instead of just making small programs... I have written lots of the code for handling input and publishing stuff on the website. Now I have come to the graphical part and I am stuck...
My idea is to have an applet with a text area. There is going to be a lot of buttons around the window. And when you push a button text will be published in the text area. My idea is to use JLabel to publish the text because then I can read HTML files and publish the HTML code directly.
However, I do not understand really how JLabels work. I have tried with "setBounds(0,100,600,800)" in order to change the size and position of the JLabel and in some way that works but not fully. Instead the program will publish the JLabel in the center of the applet. When I click the button more JLabels are published but nothing happens in the text area. Really grateful if someone could help me because I am really stuck and I don't have anyone else to ask for advice...
You can view the applet [HERE]...
/Niklas
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Applet4 extends JApplet { /**a * @param args */ public static void main(String[] args) { JFrame window = new JFrame("Simple Paint"); DrawBackground content = new DrawBackground(); window.setContentPane(content); window.setSize(1000,480); window.setLocation(0,0); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); } public void init() { setContentPane( new DrawBackground() ); } public static class DrawBackground extends JPanel implements MouseListener, MouseMotionListener { private JLabel theLabel; private Cursor previousCursor; DrawBackground () { System.out.println("Constructor"); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent (Graphics g) { JLabel aNewLabel; setBackground(Color.YELLOW); setOpaque(true); System.out.println("Background"); super.paintComponent(g); g.setColor(Color.LIGHT_GRAY); g.fillRect(10, 10, 40, 40); //Draw the text area theLabel = HtmlDisplay2(); theLabel.setBackground(Color.WHITE); theLabel.setOpaque(true); theLabel.setBounds(0,100,600,800); add(theLabel); } public void mousePressed(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { int x = evt.getX(); // x-coordinate where the user clicked. int y = evt.getY(); // y-coordinate where the user clicked. JLabel aNewLabel; if (x >= 10 && x <= 50 && y >= 10 && y <= 50) { Graphics g = getGraphics(); g.setColor(Color.BLUE); g.fillRect(10, 10, 40, 40); String testar = "<html>Testing 2</html>"; aNewLabel = new JLabel(testar); aNewLabel.setBackground(Color.WHITE); aNewLabel.setOpaque(true); aNewLabel.setBounds(0,100,600,800); add(aNewLabel); updateUI(); } } public void mouseReleased(MouseEvent evt) { } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { Graphics g = getGraphics(); int x = evt.getX(); // x-coordinate where the user clicked. int y = evt.getY(); // y-coordinate where the user clicked. // System.out.println("(" + x + ", " + y + ")"); if (x >= 10 && x <= 50 && y >= 10 && y <= 50) { g.setColor(Color.ORANGE); g.fillRect(10, 10, 40, 40); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } else { g.setColor(Color.LIGHT_GRAY); g.fillRect(10, 10, 40, 40); setCursor(previousCursor); } } public void mouseDragged(MouseEvent evt) { } private JLabel HtmlDisplay2() { JLabel aLabel; String html_text = "<html>\n" + "Color and font test:\n" + "<ul>\n" + "<li><font color=red>red</font>\n" + "<li><font color=blue>blue</font>\n" + "<li><font color=green>green</font>\n" + "<li><font size=-2>small</font>\n" + "<li><font size=+2>large</font>\n" + "<li><i>italic</i>\n" + "<li><b>bold</b>\n" + "</ul>\n"; aLabel = new JLabel(html_text); return aLabel; } } }