|
|
Исходный текст программы ListboxDemo.java
|
Оглавление |
Назад
// ==========================================
// ListboxDemo.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
public class ListboxDemo extends Applet
{
List lboxFont;
List lboxColor;
List lboxFontSize;
List lboxStyle;
Label lblFont;
Label lblColor;
Label lblFontSize;
Label lblStyle;
Color clrText = Color.black;
int nFontStyle = Font.PLAIN;
int nFontSize = 14;
Font fontTextFont;
String szFont = "Helvetica";
boolean bLargeFont = false;
// ===========================================
// init
// ===========================================
public void init()
{
setBackground(Color.yellow);
lboxFont = new List(3, false);
lboxColor = new List(5, false);
lboxFontSize = new List(3, false);
lboxStyle = new List(4, true);
lblFont = new Label("Font:");
lblColor = new Label("Color:");
lblFontSize = new Label("Font size:");
lblStyle = new Label("Font style:");
add(lblFont);
add(lboxFont);
add(lblColor);
add(lboxColor);
add(lblFontSize);
add(lboxFontSize);
add(lblStyle);
add(lboxStyle);
lboxFont.addItem("Helvetica");
lboxFont.addItem("Courier");
lboxFont.addItem("TimesRoman");
lboxFontSize.addItem("14");
lboxFontSize.addItem("16");
lboxFontSize.addItem("18");
lboxFontSize.addItem("20");
lboxFontSize.addItem("22");
lboxColor.addItem("Red");
lboxColor.addItem("Blue");
lboxColor.addItem("White");
lboxColor.addItem("Black");
lboxColor.addItem("Yellow");
lboxStyle.addItem("Large");
lboxStyle.addItem("Bold");
lboxStyle.addItem("Italic");
}
// ===========================================
// getAppletInfo
// ===========================================
public String getAppletInfo()
{
return "Name: ListboxDemo";
}
// ============================================
// paint
// ============================================
public void paint(Graphics g)
{
g.setColor(clrText);
g.setFont(fontTextFont);
g.drawString("Test string", 10, 200);
}
// ============================================
// handleEvent
// ============================================
public boolean handleEvent(Event evt)
{
List lboxChanged;
if(evt.target instanceof List)
{
lboxChanged = (List)evt.target;
if(evt.id == Event.LIST_SELECT ||
evt.id == Event.LIST_DESELECT)
{
if(lboxChanged.equals(lboxFont))
{
szFont = lboxChanged.getSelectedItem();
}
else if(lboxChanged.equals(lboxColor))
{
int nColor =
lboxColor.getSelectedIndex();
switch(nColor)
{
case 0:
{
clrText = Color.red;
break;
}
case 1:
{
clrText = Color.blue;
break;
}
case 2:
{
clrText = Color.white;
break;
}
case 3:
{
clrText = Color.black;
break;
}
case 4:
{
clrText = Color.yellow;
break;
}
}
}
else if(lboxChanged.equals(lboxStyle))
{
nFontStyle = Font.PLAIN;
bLargeFont = lboxStyle.isSelected(0);
if(lboxStyle.isSelected(1))
nFontStyle |= Font.BOLD;
if(lboxStyle.isSelected(2))
nFontStyle |= Font.ITALIC;
}
else if(lboxChanged.equals(lboxFontSize))
{
nFontSize =
14 +
2 * lboxFontSize.getSelectedIndex();
}
else
return false;
}
else
return false;
if(!bLargeFont)
fontTextFont = new Font(szFont,
nFontStyle, nFontSize);
else
fontTextFont = new Font(szFont,
nFontStyle, 2 * nFontSize);
repaint();
return true;
}
return false;
}
}
|
|
|